尝试确保 html 标签没有在 php 中保持打开状态
我无法弄清楚为什么这段代码不起作用:
<?php
$text = "<a><li><ul><ol>Hello";
$tags = array('a', 'li', 'ul', 'ol');
$tagcount = count($tags);
$i = 0;
while ($i < $tagcount) {
$opentag = "<".$tags[$i];
$closetag = "</".$tags[$i].">";
if (stripos($text, $opentag)) {
$lastopen = strripos($text, $opentag);
$lastclose = strripos($text, $closetag);
if ($lastopen > $lastclose) {
$text = substr($text, 0, $lastopen);
echo $tags[$i] . " tag was open. ";
} else {
echo $tags[$i] . " tag was closed. ";
} else {
echo $tags[$i] . " tag was not open. ";
$i++;
}
?>
它应该做的至少表明 $tags 数组中的所有标签都已打开。它的目的是使用 substr() 来确保没有标签打开,但它不起作用。运行这个给出:
标签未打开。 li 标签已打开。 ul 标签未打开。 ol 标签未打开。
即使它们都是开放的。任何帮助将不胜感激。
I cannot figure out why this code is not working:
<?php
$text = "<a><li><ul><ol>Hello";
$tags = array('a', 'li', 'ul', 'ol');
$tagcount = count($tags);
$i = 0;
while ($i < $tagcount) {
$opentag = "<".$tags[$i];
$closetag = "</".$tags[$i].">";
if (stripos($text, $opentag)) {
$lastopen = strripos($text, $opentag);
$lastclose = strripos($text, $closetag);
if ($lastopen > $lastclose) {
$text = substr($text, 0, $lastopen);
echo $tags[$i] . " tag was open. ";
} else {
echo $tags[$i] . " tag was closed. ";
} else {
echo $tags[$i] . " tag was not open. ";
$i++;
}
?>
What it should do is at least signify that all the tags in the $tags array are open. It's meant to use substr() to make sure none of the tags are open but it's not working. Running this gives:
a tag was not open. li tag was open. ul tag was not open. ol tag was not open.
even though they are all open. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来你的逻辑有缺陷:如果没有找到针,
strripos
返回false
,因此在你的内部if
语句中你正在测试一个数字是否是大于false
。对于外部
if
语句,您需要测试 false:您的内部
if
应该类似于:It seems your logic is flawed:
strripos
returnsfalse
if the needle is not found so in your innerif
statement you are testing if a number is greater thanfalse
.For your outer
if
statement, you need to test for false:Your inner
if
should be something like:是“未打开”,因为 stripos 将返回第一次出现的位置,并且第一次出现位于索引 0(计算结果为 false)。
被发现是打开的,因为它的索引不为零。但随后您截断搜索字符串,以便在零索引处找到下一次迭代
...
将 if 更改为
stripos($text, $opentag) === false
并查看是否允许您找到处于打开状态的a
标记。您必须弄清楚如何处理 substr(...) 因为我认为您的业务逻辑很可能会决定这一点。<a>
is 'not open' because stripos will return the position of the first occurrence and the first occurrence is at index 0 (which evaluates to false).<li>
is found to be open because its index is not zero. But then you truncate the search string so that the next iteration<ul>
is found at zero index...Change your if to
stripos($text, $opentag) === false
and see if that allows you to find thea
tag as being open. You will have to figure out what to do about the substr(...) since I think your business logic will most likely dictate that.下面是一个使用正则表达式的示例:
Here's an example that works using regular expressions:
解析 HTML 并不简单,有一些很好的库可以为您完成这项工作。 Tidy 库自 PHP 5 起可用,可用于解析和整齐的 HTML 片段,或完整的页面输出。有一篇关于 devzone 的很好的 文章 展示了如何使用它,包括如何将其与 输出缓冲。
对于您发布的代码,您不应该在这样的 if 语句中使用 strpos 。引用PHP手册:
因此,要测试是否在字符串中找不到子字符串,请执行以下操作:
并测试是否找到了子字符串::
但我真的、真的建议使用预先存在的库进行 HTML 操作或验证,尤其是在安全敏感的情况下(例如,反 XSS)。
Parsing HTML is non-trivial, and there are some good libraries out there to do the work for you. The Tidy library has been available since PHP 5, and can be used to parse and tidy HTML fragments, or complete page output. There's a good article on devzone which shows how to use it, including how to combine it with output buffering.
With respect to the code you've posted, you shouldn't use strpos in an if statement like that. To quote the PHP manual:
So to test that a substring was not found in a string, do:
And to test that a substring was found:
But I really, really would advise using a pre-existing library for HTML manipulation or validation, especially if it is security-sensitive (anti-XSS, for example).