PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。
由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数据被删除。
比如下述代码:
<div>string</div>string<strong><span style="COLOR: #ff0000"></span></strong>string<b>hello</b><div>string</div>
通过 strip_tags($str, ‘
<div>string</div>string<strong><span style="COLOR: #ff0000"></span></strong>stringhello<div>string</div>
而实际操作结果是这样的:
<div>string</div>string
这一切都是因为加红的那个左尖括号,查了 PHP 的文档,有一个警告提示:
由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数据被删除。
既然在执行过滤前无法验证代码正确性,遇到和标签相关的字符 “” 后面的代码就全挂了!
2013.01.11 更新:
以下方法可以解决该问题,但可能在 HTML 数据过大时,存在一定的效率问题,慎用!
function fixtags ($text) { $text = htmlspecialchars($text); $text = preg_replace("/"/", ""\"", $text); $tags = "/<(!|)(\/|)(\w*)(\ |)(\w*)([\\\=]*)(?|(\")\""\"|)(?|(.*)?"(\")|)([\ ]?)(\/|)>/i"; $replacement = ""; $text = preg_replace($tags, $replacement, $text); $text = preg_replace("/=\"\"/", "=", $text); $text = preg_replace("/"\"/", "\"", $text); return $text; }
使用方法:
strip_tags(fixtags($string), '<div>'); </div>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.