Home  >  Article  >  php教程  >  php删除html标签的三种解决方法

php删除html标签的三种解决方法

WBOY
WBOYOriginal
2016-06-08 08:47:511115browse

分享下PHP删除HTMl标签的三种方法。

方法1:
直接取出想要取出的标记

复制代码
<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">取出br标记</span>
<span style="color: #0000ff;">function</span> strip(<span style="color: #800080;">$str</span><span style="color: #000000;">)
{
</span><span style="color: #800080;">$str</span>=<span style="color: #008080;">str_replace</span>("<br>","",<span style="color: #800080;">$str</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">$str=htmlspecialchars($str);</span>
<span style="color: #0000ff;">return</span> <span style="color: #008080;">strip_tags</span>(<span style="color: #800080;">$str</span><span style="color: #000000;">);
} </span><span style="color: #008000;">//</span><span style="color: #008000;">edit by www.jbxue.com</span>
?>
复制代码

方法2.

PHP 中有个 strip_tags 函数可以方便地去除 HTML 标签。
echo strip_tags(“Hello World”); // 去除 HTML、XML 以及 PHP 的标签。
对于非标准的 HTML 代码也能正确的去除:
echo strip_tags(“\”>cftea”); //输出 cftea
在PHP中可以使用strip_tags函数去除HTML标签,看下面示例:

<span style="color: #000000;">php
</span><span style="color: #800080;">$str</span> = ‘www.<p>jbxue</p>.com'<span style="color: #000000;">;
echo(htmlspecialchars($str).”<br>”);
echo(strip_tags($str));
?></span>

方法3.

strtr() 函数转换字符串中特定的字符。
语法
strtr(string,from,to)
或者
strtr(string,array)
参数 描述
string1 必需。规定要转换的字符串。
from 必需(除非使用数组)。规定要改变的字符。
to 必需(除非使用数组)。规定要改变为的字符。
array 必需(除非使用 from 和 to)。一个数组,其中的键是原始字符,值是目标字符。

例子1:

<span style="color: #000000;">php
</span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">strtr</span>("Hilla Warld","ia","eo"<span style="color: #000000;">);
</span>?>

例子2:

<span style="color: #000000;">php
</span><span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span>("Hello" => "Hi", "world" => "earth"<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">strtr</span>("Hello world",<span style="color: #800080;">$arr</span><span style="color: #000000;">);
</span>?>
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