百科狗-知识改变命运!
--

strip_tags() - 从字符串中去除 HTML 和 PHP 标记 - php 字符串函数

乐乐1年前 (2023-11-21)阅读数 12#技术干货
文章标签字符串

strip_tags()

(PHP 4, PHP 5, PHP 7)

从字符串中去除 HTML 和 PHP 标记

说明

strip_tags(string $str[,string $allowable_tags] ): string

该函数尝试返回给定的字符串$str去除空字符、HTML 和 PHP 标记后的结果。它使用与函数fgetss()一样的机制去除标记。

参数

$str

strip_tags() - 从字符串中去除 HTML 和 PHP 标记 - php 字符串函数

输入字符串。

$allowable_tags

使用可选的第二个参数指定不被去除的字符列表。

Note:

HTML 注释和 PHP 标签也会被去除。这里是硬编码处理的,所以无法通过$allowable_tags参数进行改变。Note:

In PHP 5.3.4 and later, self-closing XHTML tags are ignored and only non-self-closing tags should be used in$allowable_tags. For example, to allow both
and
, you should use:

返回值

返回处理后的字符串。

更新日志

版本说明
5.3.4strip_tags()ignores self-closing XHTML tags in$allowable_tags.
5.0.0strip_tags()变为二进制安全的。

范例

Example #1strip_tags()范例

以上例程会输出:

Test paragraph. Other text

Test paragraph.

Other text

注释

Warning

由于strip_tags()无法实际验证 HTML,不完整或者破损标签将导致更多的数据被删除。

Warning

该函数不会修改$allowable_tags参数中指定的允许标记的任何属性,包括styleonmouseover属性,用户可能会在提交的内容中恶意滥用这些属性,从而展示给其他用户。

Note:

输入 HTML 标签名字如果大于 1023 字节(bytes)将会被认为是无效的,无论$allowable_tags参数是怎样的。

参见

  • htmlspecialchars()将特殊字符转换为 HTML 实体
Hi. I made a function that removes the HTML tags along with their contents:
Function:

Sample text:
$text = 'sample text with 
tags
'; Result for strip_tags($text): sample text with tags Result for strip_tags_content($text): text with Result for strip_tags_content($text, ''): sample text with Result for strip_tags_content($text, '', TRUE); text with
tags
I hope that someone is useful :)
https://bugs.php.net/bug.php?id=78346
After upgrading from v7.3.3 to v7.3.7 it appears nested "php tags" inside a string are no longer being stripped correctly by strip_tags().
This is still working in v7.3.3, v7.2 & v7.1. I've added a simple test below.
Test script:
---------------
a HTML code like this: 

with 
... the result is:
$str = 'color is bluesize is huge
material is wood'; 
notice: the words 'blue' and 'size' grow together :( 
and line-breaks are still in new string $str
if you need a space between the words (and without line-break) 
use my function: 
... the result is:
$str = 'color is blue size is huge material is wood'; 
the function: 

the KEY is the regex pattern: '/]*>/'
instead of strip_tags() 
... then remove control characters and multiple spaces
:)
"5.3.4  strip_tags() no longer strips self-closing XHTML tags unless the self-closing XHTML tag is also given in allowable_tags."
This is poorly worded.
The above seems to be saying that, since 5.3.4, if you don't specify "
" in allowable_tags then "
" will not be stripped... but that's not actually what they're trying to say. What it means is, in versions prior to 5.3.4, it "strips self-closing XHTML tags unless the self-closing XHTML tag is also given in allowable_tags", and that since 5.3.4 this is no longer the case. So what reads as "no longer strips self-closing tags (unless the self-closing XHTML tag is also given in allowable_tags)" is actually saying "no longer (strips self-closing tags unless the self-closing XHTML tag is also given in allowable_tags)". i.e. pre-5.3.4: strip_tags('Hello World

','
') => 'Hello World
' // strips
because it wasn't explicitly specified in allowable_tags 5.3.4 and later: strip_tags('Hello World

','
') => 'Hello World

' // does not strip
because PHP matches it with
in allowable_tags
Note the different outputs from different versions of the same tag:
A word of caution. strip_tags() can actually be used for input validation as long as you remove ANY tag. As soon as you accept a single tag (2nd parameter), you are opening up a security hole such as this:

Plus: regexing away attributes or code block is really not the right solution. For effective input validation when using strip_tags() with even a single tag accepted, http://htmlpurifier.org/ is the way to go.
Features:
* allowable tags (as in strip_tags),
* optional stripping attributes of the allowable tags,
* optional comment preserving,
* deleting broken and unclosed tags and comments,
* optional callback function call for every piece processed allowing for flexible replacements.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)