php DomDocument中的getElementsByTagName错误

时间:2018-01-22 13:06:37

标签: php domdocument

php上有此代码。在DOMDocument我搜索所有img代码后,我会在其上方进行一次循环并进行更改。

html中有5个标签img。循环过了3次。

如何解决这个问题?

$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$imgs_t = $dom->getElementsByTagName('img');
$amp_img_src = '/images/front/what_is_dt_video.jpg';
$count_foreach = 0;
foreach( $imgs_t as $img_t ){
    $count_foreach++;
    $img_alt = '';
    $tmp_img_src = $img_t->getAttribute('src');
    $img_alt = $img_t->getAttribute('alt');

    if($tmp_img_src != $amp_img_src){                
        $tmp_amp_img = $dom->createElement('amp-img');
        $tmp_amp_img->setAttribute('width', 200);
        $tmp_amp_img->setAttribute('height', 200);
        $tmp_amp_img->setAttribute('src', $tmp_img_src);
        $tmp_amp_img->setAttribute('alt', $img_alt);

        $img_t->parentNode->replaceChild($tmp_amp_img, $img_t);
    }
}
var_dump($count_foreach);

这正是在{php}代码开头输入html变量的$html

<div class="what_is_dt drop_down_pg">
    <div class="drop_down_video drop_down_cont">
        <div class="col-sm-7 col-xs-12">
            <div class="video_wrap">
                <div id="ntx_dt_info_video_link" class="no-padding">
                    <img src="/images/front/what_is_dt_video.jpg" height="315px" width="481px" alt="Lorem ipsum">
                </div>
                <div class="what_fx_video_head">
                    <span>Lorem ipsum</span>
                </div>
            </div>
        </div>
        <div class="col-sm-5 col-xs-12 no-padding">

            <span class="background_green">

                <h2>Lorem ipsum</h2>
                <p><em>Lorem ipsum</em></p>
                <p><em>Lorem ipsum</em></p>
            </span>
            [T]CONFIRM_THEORY[/T]
        </div>
        [T]CONFIRM_THEORY_MOBILE[/T]
        <hr />
    </div>
    <h2 style="margin-top: 40px;"><strong>Lorem ipsum</strong></h2>
    <div style="padding-top: 30px;">
        <div class="signature-left col-lg-6 col-md-6 col-sm-6 col-xs-12">
            <img src="/uploads/docs/th%20Failure%20Swing.png" alt="Lorem ipsum" title="Lorem ipsum" width="200" height="200" />
            <span style="font-size: small;"> <strong>Lorem ipsum.</strong></span>
            <span style="font-size: small;">Lorem ipsum. </span>
        </div>
        <div class="signature-left col-lg-6 col-md-6 col-sm-6 col-xs-12">
            <img src="/uploads/docs/th%20Nonfailure%20Swing.png" alt="Lorem ipsum" width="200" height="200"/>
            <span style="font-size: small;"><strong>Lorem ipsum.</strong> </span>
            <span style="font-size: small;">Lorem ipsum.</span>
        </div>
    </div>
    <p style="clear: both; padding-top: 30px;" >Lorem ipsum.</p>
    <div style="padding-top: 30px;">
        <div class="signature-left col-lg-6 col-md-6 col-sm-6 col-xs-12">
            <img src="/uploads/docs/th Failure Swing Bottom.png" alt="" />
            <p><span style="font-size: small;"><strong>Failure Swing </strong>Lorem ipsum.</span></p>
        </div>
        <div class="signature-left col-lg-6 col-md-6 col-sm-6 col-xs-12">
            <img src="/uploads/docs/th Nonfailure Swing Bottom.png" alt="Lorem ipsum" width="200" height="200" />
            <p><span style="font-size: small;"><strong>Nonfailure Swing</strong>Lorem ipsum.</span></p>
        </div>
    </div>
    <p style="clear: both;"><br /></p>
    [T]OUR_LEARNING[/T]
</div>

0 个答案:

没有答案
相关问题