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

How to remove content within tags in php

藏色散人
藏色散人Original
2021-07-26 09:41:101852browse

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;

a80eb7cbb6fff8b0ff70bae37074b813
feec247b2ba7cd690f412362b049d2dce388a4556c0f65e1904146cc1a846bee这里是p标签94b3e26ee717c64999d7867364b1b4a315f9448f161df2127944e707d9cd02376170e8999ab19ec641e0422470b16d5c这里是a标签5db79b134e9f6b82c0b36e0489ee08ed0c6dc11e160d3b678d68754cc175188a94b3e26ee717c64999d7867364b1b4a3';

function strip_html_tags($tags,$str){ 
    $html=array();
    foreach ($tags as $tag) {
        $html[]='/c3f928d2173e20cfad2b48ee50097c20[\s|\S]*?821d8ae39ab5af39b59100bff1388f39/';
        $html[]='/c3f928d2173e20cfad2b48ee50097c20/';
    }
    $data=preg_replace($html,'',$str);
    return $data;
} 
echo strip_html_tags(array('a','img'),$str);
//输出e388a4556c0f65e1904146cc1a846beee388a4556c0f65e1904146cc1a846bee这里是p标签94b3e26ee717c64999d7867364b1b4a30c6dc11e160d3b678d68754cc175188a94b3e26ee717c64999d7867364b1b4a3
?>

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>;
?>

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!

Statement:
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