为什么在下面的场景中控件没有进入foreach内部


Why the control is not going inside foreach in following scenario?

场景是我从MySQL数据库中获取一条记录。这个记录数据是HTML形式的,所以我想从这个HTML数据记录中删除一些标记。为此,我还编写了一个函数。但我不明白为什么控制没有进入前臂内侧。为了供您参考,我在下面给出了一些功能:

function clear_question_data($html){ 
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    foreach($dom->getElementsByTagName('img') as $image)
    { echo "Inside Foreach"; die;
        $image->removeAttribute('alt');
        $image->removeAttribute('xmlns');
        $image->removeAttribute('title');
    }
            echo "Out of Foreach"; die;
        $txt=$dom->saveHTML();
        $dom->loadHTML($txt);
        foreach($dom->getElementsByTagName('img') as $image)
        {
            $srcval=$image->getAttribute('src');
            $srcval = htmlspecialchars_decode($srcval);
            $srcval = str_replace(' ', ' ', $srcval);  
            if(strpos($srcval,"%5C%22")==0)
            {           
                $srcval = str_replace("%5C%22", "", $srcval);
                $srcval = str_replace(".png%5C%22", ".png", $srcval);
            }
            if(strpos($srcval,"../../..")==0)
            {           
                $srcval = str_replace("../../..", "", $srcval);
            }
            if(strpos($srcval,"../..")==0)
            {           
                $srcval = str_replace("../..", "", $srcval);
            }
            if(strpos($srcval,"/ckeditor_3.6.1//plugins")==0) 
            {           
                $srcval = str_replace("/ckeditor_3.6.1//", EPN_SITE_URL."ckeditor_3.6.1/", $srcval);
            }

              $srcval = str_replace(".png/'"", ".png", $srcval);
              $srcval = str_replace("�", "-", $srcval);
            $image->setAttribute('src',$srcval);
        }   
        $final_data=$dom->saveHTML();
        return $final_data;
}

样本输入数据(即$html)如下:

Glucose when hetaed with CH<sub>3</sub>OH in presence of dry HCl gas gives<img align='"middle'" alt='"«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mi»§#945;«/mi»«/math»'" class='"Wirisformula'" src='"/ckeditor_3.6.1//plugins/ckeditor_wiris/integration/showimage.php?formula=dedbf6a559a928eeeaee82c4b1bf40d3.png'" title='"Double click to edit'"> and <img align='"middle'" alt='"«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mi»§#946;«/mi»«/math»'" class='"Wirisformula'" src='"/ckeditor_3.6.1//plugins/ckeditor_wiris/integration/showimage.php?formula=2c5cf4a4494a03be06d6c32308a225ba.png'" title='"Double click to edit'">-methyl glycosides because it contains.<br>

每当我尝试调试这个函数时,我都会收到一条消息"Out of Foreach",而不是"Inside Foreach"。我不明白为什么会发生这种事。有人能在这方面帮我吗?任何形式的帮助都将不胜感激。如果你有比foreach更好的方法来实现这个结果,那是受欢迎的。

尽管你说加载没有问题,但请尝试这样加载,因为代码格式错误:

$dom = new DOMDocument;
$dom->strictErrorChecking = FALSE;
$dom->loadHTML($html);