How to remove html in php

藏色散人
Release: 2023-03-07 10:08:01
Original
2633 people have browsed it

php去掉html标签的方法:1、通过“function strip($str){...}”方法删除HTMl标签;2、通过“strip_tags”函数去除HTML标签;3、通过strtr函数转换字符串中特定的字符。

How to remove html in php

推荐:《PHP视频教程

PHP删除HTMl标签的三种解决方法

方法1:

直接取出想要取出的标记

代码如下:

","",$str); //$str=htmlspecialchars($str); return strip_tags($str); } ?>
Copy after login

方法2.

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

echo strip_tags(“Hello World”); // 去除 HTML、XML 以及 PHP 的标签。
Copy after login

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

echo strip_tags(“\\”>cftea”); //输出 cftea
Copy after login

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

代码如下:

dreamdu

.com'; echo(htmlspecialchars($str).”
”); echo(strip_tags($str)); ?>
Copy after login

方法3.

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

语法

strtr(string,from,to)
Copy after login

或者

strtr(string,array)
Copy after login

参数

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

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

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

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

例子1:

代码如下:

Copy after login

例子2:

代码如下:

 "Hi", "world" => "earth"); echo strtr("Hello world",$arr); ?>
Copy after login

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

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!