Home  >  Article  >  Backend Development  >  How to delete html tags in php

How to delete html tags in php

藏色散人
藏色散人Original
2020-07-06 10:15:083803browse

php删除html的方法:1、通过自定义的“strip”方法取出html标记;2、使用“strip_tags”函数去除HTML等标签;3、利用“strtr”函数转换字符串中特定的字符,用以删除html相关标签。

How to delete html tags in php

php删除html标签

方法1:

直接取出想要取出的标记

代码如下:

<?php
    //取出br标记
    function strip($str)
{
$str=str_replace("&lt;br&gt;","",$str);
//$str=htmlspecialchars($str);
return strip_tags($str);
}
?>

方法2:

PHP 中有个 strip_tags 函数可以方便地去除 HTML 标签。

echo strip_tags(“Hello <b>World</b>”); // 去除 HTML、XML 以及 PHP 的标签。

对于非标准的 HTML 代码也能正确的去除:

echo strip_tags(“<a href=\”>\”>cftea</a>”); //输出 cftea

在PHP中可以使用strip_tags函数去除HTML标签,看下面示例:

代码如下:

<?php
$str = ‘www<p>dreamdu</p>.com&#39;;
echo(htmlspecialchars($str).”<br>”);
echo(strip_tags($str));
?>

方法3:

strtr() 函数转换字符串中特定的字符。

语法

strtr(string,from,to)

或者

strtr(string,array)

参数 

string1 必需。规定要转换的字符串。

from 必需(除非使用数组)。规定要改变的字符。

to 必需(除非使用数组)。规定要改变为的字符。

array 必需(除非使用 from 和 to)。一个数组,其中的键是原始字符,值是目标字符。

例子1:

代码如下:

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

例子2:

代码如下:

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

很多相关知识,请访问PHP中文网

The above is the detailed content of How to delete html tags in php. For more information, please follow other related articles on the PHP Chinese website!

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