Home > php教程 > php手册 > php删除html标签的三种解决方法,phphtml标签三种

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

WBOY
Release: 2016-06-13 08:38:42
Original
1214 people have browsed it

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

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

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

<?<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>
?>
Copy after login

方法2.

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

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

方法3.

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

例子1:

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

例子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>?>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template