php函数strip_tags标签未闭合的情况

来源:互联网 发布:opencv python 书 编辑:程序博客网 时间:2024/04/29 07:53

在前一阵在的面试过程中面试官曾经问我,如果字符串中的标签未闭合,strip_tags会如何处理?


在php的官方文档中对strip_tags的表述如下;

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

注意文档下方的warning:

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


这句话是什么意思呢?

通过下面的几个案例来向大家介绍

sample1:

$testStr1 = "<strong>hello world!</strong>";echo strip_tags($testStr1);

输出:

hello world!

sample2:

$testStr1 = "<strong>hello world!";echo strip_tags($testStr1);
输出:
hello world!


sample3:

$testStr2 = "<stronghello world!</strong>";echo strip_tags($testStr2);

输出空字符串


sample4:

$testStr3 = "hello world1<stronghello world2!</strong>hello world3";echo strip_tags($testStr3);


输出:

hello world1


通过以上几个案例我们可以看出在处理没有闭合的标签时strip_tags函数会将未闭合标签之后的所有字符串全部清除。



0 0
原创粉丝点击