开我想不通为什么这个代码不工作:努力确保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++;
}
?>
它应该做的是至少表明了$标签阵列中的所有标签是开放的。它的意思是使用substr()来确保没有标签是打开的,但它不起作用。运行此产生:
标记未打开。 li标签已打开。 ul标签未打开。 ol标签未打开。
尽管它们都是开放的。任何帮助将不胜感激。