PHP Three solutions for removing HTMl tags

  • 2020-06-23 00:01:19
  • OfStack

Method 1:
Take out the mark you want directly


<?php
    // Take out the br tag 
    function strip($str)
{
$str=str_replace("&lt;br&gt;","",$str);
//$str=htmlspecialchars($str);
return strip_tags($str);
}
?>

Method 2.
There is an strip_tags function in PHP to conveniently remove HTML tags.
echo strip_tags (" Hello < b > World < /b > "); // Remove HTML, XML and PHP labels.
Non-standard HTML code can also be removed correctly:
echo strip_tags (" < a href = \" > \" > cftea < /a > "); / / output cftea
In PHP, you can use the strip_tags function to remove the HTML tag, as shown in the following example:

<?php
$str =  ' www<p>dreamdu</p>.com';
echo(htmlspecialchars($str). " <br> " );
echo(strip_tags($str));
?>

Method 3.
The strtr() function converts a specific character in a string.
grammar
strtr(string,from,to)
or
strtr(string,array)
parameter describe string1 A necessity. Specifies the string to be converted. from Required (unless using arrays). Specifies the character to change. to Required (unless using arrays). Specifies the character to be changed to. array Required (unless from and to are used). 1 array where the key is the original character and the value is the target character.
Example 1:

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

Example 2:

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


Related articles: