開我想不通爲什麼這個代碼不工作:努力確保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標籤未打開。
儘管它們都是開放的。任何幫助將不勝感激。