Home >Backend Development >PHP Tutorial >Three solutions to delete html tags in php, three phphtml tags_PHP tutorial
Share three methods to delete HTMl tags in PHP.
Method 1:
Directly take out the mark you want to take out
<?<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> ?>
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> = ‘www.<p>jbxue</p>.com'<span>; echo(htmlspecialchars($str).”<br>”); 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>?>