Home  >  Article  >  Backend Development  >  Three solutions to delete html tags in php, three phphtml tags_PHP tutorial

Three solutions to delete html tags in php, three phphtml tags_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:51:19782browse

Three solutions to delete html tags in php, three ways to delete html tags in php

Share three methods to delete HTMl tags in PHP.

Method 1:
Directly take out the mark you want to take out

Three solutions to delete html tags in php, three phphtml tags_PHP tutorial
<?<span>php
</span><span>//</span><span>取出br标记</span>
<span>function</span> strip(<span>$str</span><span>)
{
</span><span>$str</span>=<span>str_replace</span>("<br>","",<span>$str</span><span>);
</span><span>//</span><span>$str=htmlspecialchars($str);</span>
<span>return</span> <span>strip_tags</span>(<span>$str</span><span>);
} </span><span>//</span><span>edit by www.jbxue.com</span>
?>
Three solutions to delete html tags in php, three phphtml tags_PHP tutorial

Method 2.

There is a strip_tags function in PHP that can easily remove HTML tags.
echo strip_tags(“Hello World”); // Remove HTML, XML and PHP tags.
Non-standard HTML codes can also be removed correctly:
echo strip_tags(“”>cftea”); //Output cftea
in PHP You can use the strip_tags function to remove HTML tags, see the following example:

<?<span>php
</span><span>$str</span> = &lsquo;www.<p>jbxue</p>.com'<span>;
echo(htmlspecialchars($str).&rdquo;<br>&rdquo;);
echo(strip_tags($str));
?></span>

Method 3.

strtr() function converts specific characters in a string.
Syntax
strtr(string,from,to)
or
strtr(string,array)
Parameter Description
string1 Required. Specifies the string to be converted.
from Required (unless using an array). Specifies the character to be changed.
to Required (unless using an array). Specifies the character to change to.
array required (unless from and to are used). An array where the keys are the original characters and the values ​​are the target characters.

Example 1:

<?<span>php
</span><span>echo</span> <span>strtr</span>("Hilla Warld","ia","eo"<span>);
</span>?>

Example 2:

<?<span>php
</span><span>$arr</span> = <span>array</span>("Hello" => "Hi", "world" => "earth"<span>);
</span><span>echo</span> <span>strtr</span>("Hello world",<span>$arr</span><span>);
</span>?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1131874.htmlTechArticleThree solutions to delete html tags in PHP, three ways to delete HTMl tags in PHP . Method 1: Directly remove the tag you want to remove ? php // Remove the br tag f...
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