Home > Backend Development > PHP Problem > How to remove content within tags in php

How to remove content within tags in php

藏色散人
Release: 2023-03-11 19:42:02
Original
1853 people have browsed it

php method to remove content in tags: 1. Use the "strip_html_tags($tags,$str);" method to remove; 2. Use the "strip_html_tags($tags,$str,$content);" method Remove.

How to remove content within tags in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to remove the tag inside php Content?

PHP method to delete html tags and the content within the tag

  • Delete the tag and the content of the tag

Usage method: strip_html_tags($tags,$str);
$tags: Tags that need to be deleted (array format)
$str: String that needs to be processed;

<meta charset="UTF-8">
<?php
$str=&#39;<p><p>这里是p标签</p><img src="" alt="这里是img标签"><a href="">这里是a标签</a><br></p>';

function strip_html_tags($tags,$str){ 
    $html=array();
    foreach ($tags as $tag) {
        $html[]='/<&#39;.$tag.&#39;.*?>[\s|\S]*?<\/&#39;.$tag.&#39;>/';
        $html[]='/<&#39;.$tag.&#39;.*?>/';
    }
    $data=preg_replace($html,'',$str);
    return $data;
} 
echo strip_html_tags(array('a','img'),$str);
//输出<p><p>这里是p标签</p><br></p>
?>
Copy after login

Effect picture:


  • Ultimate function, delete the specified tag; delete or retain the content in the tag;
    Usage: strip_html_tags($tags,$str ,$content);
    $tags: tags that need to be deleted (array format)
    $str: string that needs to be processed;
    $ontent: whether to delete the content in the tag 0 retain the content 1 do not retain the content

<meta charset="UTF-8">
<?php
$str=&#39;<p><p>这里是p标签</p><img src="" alt="这里是img标签"><a href="">这里是a标签</a><br></p>&#39;;

/**
 * 删除指定的标签和内容
 * @param array  $tags 需要删除的标签数组
 * @param string $str 数据源
 * @param boole  $content 是否删除标签内的内容 默认为false保留内容  true不保留内容
 * @return string
 */
function strip_html_tags($tags,$str,$content=false){
    $html=array();
    foreach ($tags as $tag) {
        if($content){
            $html[]=&#39;/(<&#39;.$tag.&#39;.*?>[\s|\S]*?<\/&#39;.$tag.&#39;>)/&#39;;
        }else{
            $html[]="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i";
        }
    }
    $data=preg_replace($html, &#39;&#39;, $str);
    return $data;
}
echo strip_html_tags(array(&#39;a&#39;),$str,1);
//输出<p><p>这里是p标签</p><img src="" alt="这里是img标签"><br></p>;
?>
Copy after login

Rendering:

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to remove content within tags in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template