How to remove div tags in php: 1. Remove the br tag through "function strip($str){...}"; 2. Use the strip_tags function to remove HTML tags; 3. Use the strtr function to convert the string specific characters.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
How to remove div tags with PHP? Delete with PHP Three solutions to HTMl tags
Method 1:
Directly take out the tag you want to take out
The code is as follows:
<?php //取出br标记 function strip($str) { $str=str_replace("<br>","",$str); //$str=htmlspecialchars($str); return strip_tags($str); } ?>
Method 2 .
There is a strip_tags function in PHP that can easily remove HTML tags.
echo strip_tags(“Hello <b>World</b>”); // 去除 HTML、XML 以及 PHP 的标签。
Non-standard HTML codes can also be removed correctly:
echo strip_tags(“<a href=\”>\”>cftea</a>”); //输出 cftea
In PHP, you can use the strip_tags function to remove HTML tags, see the example below:
The code is as follows:
<?php $str = ‘www<p>dreamdu</p>.com'; echo(htmlspecialchars($str).”<br>”); echo(strip_tags($str)); ?>
Method 3.
strtr() function converts specific characters in the string.
Syntax
strtr(string,from,to)
or
strtr(string,array)
Parameters
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:
The code is as follows:
<?php echo strtr("Hilla Warld","ia","eo"); ?>
Example 2:
The code is as follows:
<?php $arr = array("Hello" => "Hi", "world" => "earth"); echo strtr("Hello world",$arr); ?>
Recommended study:《PHP video tutorial》
The above is the detailed content of How to remove div tags in php. For more information, please follow other related articles on the PHP Chinese website!