2

我正在尝试用适当的文本替换某些满足特定要求的 HTML 中的所有图像。具体要求是它们属于“replaceMe”类,并且图像 src 文件名在 $myArray 中。在寻找解决方案时,似乎某种 PHP DOM 技术是合适的,但是,我对此很陌生。例如,给定 $html,我希望返回 $desired_html。在这篇文章的底部是我尝试的实现,目前不起作用。谢谢

$myArray=array(
    'goodImgage1'=>'Replacement for Good Image 1',
    'goodImgage2'=>'Replacement for Good Image 2'
);

$html = '<div>
<p>Random text and an <img src="goodImgage1.png" alt="" class="replaceMe">.  More random text.</p>
<p>Random text and an <img src="goodImgage2.png" alt="" class="replaceMe">.  More random text.</p>
<p>Random text and an <img src="goodImgage2.png" alt="" class="dontReplaceMe">.  More random text.</p>
<p>Random text and an <img src="badImgage1.png"  alt="" class="replaceMe">.  More random text.</p>
</div>';

$desiredHtml = '<div>
<p>Random text and an Replacement for Good Image 1.  More random text.</p>
<p>Random text and an Replacement for Good Image 2.  More random text.</p>
<p>Random text and an <img src="goodImgage2.png" alt="" class="dontReplaceMe">.  More random text.</p>
<p>Random text and an <img src="badImgage1.png"  alt="" class="replaceMe">.  More random text.</p>
</div>';

以下是我正在尝试做的事情..

libxml_use_internal_errors(true);   //Temorarily disable errors resulting from improperly formed HTML
$doc = new DOMDocument();
$doc->loadHTML($html);

//What does this do for me?
$imgs= $doc->getElementsByTagName('img');
foreach ($imgs as $img){}

$xpath = new DOMXPath($doc);
foreach( $xpath->query( '//img') as $img) {
    if(true){   //How do I check class and image name?
        $new = $doc->createTextNode("New Attribute"); 
        $img->parentNode->replaceChild($new,$img);
    }
}

$html=$doc->saveHTML();
libxml_use_internal_errors(false);
4

2 回答 2

1

这样做,你的方式很好:

$myArray=array(
    'goodImgage1.png'=>'Replacement for Good Image 1',
    'goodImgage2.png'=>'Replacement for Good Image 2'
);

$html = '<div>
<p>Random text and an <img src="goodImgage1.png" alt="" class="replaceMe">.  More random text.</p>
<p>Random text and an <img src="goodImgage2.png" alt="" class="replaceMe">.  More random text.</p>
<p>Random text and an <img src="goodImgage2.png" alt="" class="dontReplaceMe">.  More random text.</p>
<p>Random text and an <img src="badImgage1.png"  alt="" class="replaceMe">.  More random text.</p>
</div>';

$classesToReplace = array('replaceMe');

libxml_use_internal_errors(true);   //Temorarily disable errors resulting from improperly formed HTML
$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXPath($doc);
foreach( $xpath->query( '//img') as $img) {
    // get the classes into an array
    $classes = explode(' ', $img->getAttribute('class')); // this will contain the classes assigned to the element
    $classMatches = array_intersect($classes, $classesToReplace);

    // preprocess the image name to match the $myArray keys
    $imageName = $img->getAttribute('src');

    if (isset($myArray[$imageName]) && $classMatches) {   
        $new = $doc->createTextNode($myArray[$imageName]); 
        $img->parentNode->replaceChild($new,$img);
    }
}

echo var_dump($html = $doc->saveHTML());

请注意以下事项:

  • 我对具有该类的图像进行了代码检查replaceMe,可能除了其他类
  • 我将完整的图像文件名添加到您的$myArray密钥中,基本上是为了简单起见。
于 2013-05-19T19:42:10.530 回答
1

likeitlikeit更快。不过,我会发布我的答案,因为它在细节上有一些差异,例如xpath<img>使用适当的class属性进行选择,使用pathinfo来获取不带扩展名的文件名。

$doc = new DOMDocument();
$doc->loadHTML($h); // assume HTML in $h

$xpath = new DOMXPath($doc);
$imgs = $xpath->query("//img[@class = 'replaceMe']");

foreach ($imgs as $img) {

    $imgfile = pathinfo($img->getAttribute("src"),PATHINFO_FILENAME);
    if (array_key_exists($imgfile, $myArray)) { 

        $replacement = $doc->createTextNode($myArray[$imgfile]);
        $img->parentNode->replaceChild($replacement, $img); 
    }
}

echo "<pre>" . htmlentities($doc->saveHTML()) . "</pre>";

看到它工作:http ://codepad.viper-7.com/11XZt7

于 2013-05-19T20:02:54.100 回答