Home > Backend Development > PHP Tutorial > PHP example code for batch replacement of html tags_PHP tutorial

PHP example code for batch replacement of html tags_PHP tutorial

WBOY
Release: 2016-07-13 17:17:41
Original
966 people have browsed it

1. Remove all html elements, or keep certain html tags

Copy the code The code is as follows:

$text = '

Test paragraph.

Other text ';
echo strip_tags($text);
echo "/n";

// Allow

and
echo strip_tags($text, '

');
?>


The result is (with comments removed):

Test paragraph. Other text

Test paragraph.

Other text
2 .On the contrary, only remove a certain html tag

Copy code The code is as follows:

function strip_only ($str, $tags, $stripContent = false) {
$content = '';
if(!is_array($tags)) {
$tags = (strpos($str, '> ') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
If(end($tags) == '' ) array_pop($tags);
}
foreach($tags as $tag) {
if ($stripContent)
[^>]*>|)';
                       $str = preg_replace('#]*>'.$content.'#is', '', $str);
}
return $str;
}

$str = 'red text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621724.htmlTechArticle1. Remove all html elements, or keep certain html tags. Copy the code as follows: ?php $text = 'pTest paragraph./p!-- Comment -- a href="#fragment"Other text/a'; echo strip_...
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