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.
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='<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[]='/<'.$tag.'.*?>[\s|\S]*?<\/'.$tag.'>/'; $html[]='/<'.$tag.'.*?>/'; } $data=preg_replace($html,'',$str); return $data; } echo strip_html_tags(array('a','img'),$str); //输出<p><p>这里是p标签</p><br></p> ?>
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='<p><p>这里是p标签</p><img src="" alt="这里是img标签"><a href="">这里是a标签</a><br></p>'; /** * 删除指定的标签和内容 * @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[]='/(<'.$tag.'.*?>[\s|\S]*?<\/'.$tag.'>)/'; }else{ $html[]="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i"; } } $data=preg_replace($html, '', $str); return $data; } echo strip_html_tags(array('a'),$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!