我无法弄清楚为什么这段代码不起作用:
<?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 标签未打开。
即使它们都是开放的。任何帮助将不胜感激。