Home >Backend Development >PHP Problem >How to delete specified html tags in php
php method to delete specified html tags: 1. Use strip_tags() function, syntax "strip_tags(string,allow)"; 2. Use strip_html_tags() function, syntax "strip_html_tags(tags,str)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php delete the specified html Tag method
$str='e388a4556c0f65e1904146cc1a846beee388a4556c0f65e1904146cc1a846bee这里是p标签94b3e26ee717c64999d7867364b1b4a315f9448f161df2127944e707d9cd02376170e8999ab19ec641e0422470b16d5c这里是a标签5db79b134e9f6b82c0b36e0489ee08ed0c6dc11e160d3b678d68754cc175188a94b3e26ee717c64999d7867364b1b4a3';
1: Delete all or keep the specified html tags
The function strip_tags that comes with php can meet the requirements,
Usage:
strip_tags(string,allow)
string: the string that needs to be processed;
allow: the specified tag that needs to be retained, you can write Multiple;
echo strip_tags($str,'e388a4556c0f65e1904146cc1a846bee3499910bf9dac5ae3c52d5ede7383485');//输出:e388a4556c0f65e1904146cc1a846bee这里是p标签94b3e26ee717c64999d7867364b1b4a36170e8999ab19ec641e0422470b16d5c这里是a标签5db79b134e9f6b82c0b36e0489ee08ed
But the shortcomings are also obvious;
If there are a lot of tags;
And I just want to delete the specified one;
That requires writing a lot of tags that need to be retained;
So there is a second method;
2: Delete the specified html tag
Usage:
strip_html_tags($tags,$str);
$tags: Tags that need to be deleted (array format)
$str: the string that needs to be processed;
function strip_html_tags($tags,$str){ $html=array(); foreach ($tags as $tag) { $html[]="/(c8a6421ee9cab4b2a89ed0b67e4174e1]*>)/i"; } $data=preg_replace($html, '', $str); return $data; } echo strip_html_tags(array('p','img'),$str); //输出:dc6dce4a544fdca2df29d5ac0ea9906b这里是p标签6170e8999ab19ec641e0422470b16d5c这里是a标签5db79b134e9f6b82c0b36e0489ee08ed0c6dc11e160d3b678d68754cc175188a16b28748ea4df4d9c2150843fecfba68;
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to delete specified html tags in php. For more information, please follow other related articles on the PHP Chinese website!