0

我正在创建一个 wordpress 函数,需要确定内容中的图像是否用包含指向 PDF 或 DOC 文件的链接的标签包装,例如

<a href="www.site.com/document.pdf"><img src="../images/image.jpg" /></a>

我将如何使用 PHP 进行此操作?

谢谢

4

2 回答 2

2

强烈建议不要为此使用正则表达式。除了更容易出错和可读性更低之外,它也不能让您轻松操作内容。

您最好将内容加载到 DomDocument 中,检索所有<img>元素并验证其父元素是否为<a>元素。然后,您要做的就是验证href属性的值是否以所需的扩展名结尾。

一个非常粗略的实现看起来有点像这样

<?php

$sHtml = <<<HTML
<html>
<body>
    <img src="../images/image.jpg" />
    <a href="www.site.com/document.pdf"><img src="../images/image.jpg" /></a>
    <a href="www.site.com/document.txt"><img src="../images/image.jpg" /></a>
    <p>this is some text <a href="site.com/doc.pdf"> more text</p> 
</body>
</html>
HTML;

$oDoc = new DOMDocument();
$oDoc->loadHTML($sHtml);
$oNodeList = $oDoc->getElementsByTagName('img');

foreach($oNodeList as $t_oNode)
{
    if($t_oNode->parentNode->nodeName === 'a')
    {
        $sLinkValue = $t_oNode->parentNode->getAttribute('href');
        $sExtension = substr($sLinkValue, strrpos($sLinkValue, '.'));

        echo '<li>I am wrapped in an anchor tag '
           . 'and I link to  a ' . $sExtension . ' file '
        ; 
    }
}
?>

我将为读者留下一个确切的实现作为练习;-)

于 2013-04-18T11:02:49.440 回答
0

这是您可以使用的基于 DOM 解析的代码:

$html = <<< EOF
<a href="www.site.com/document.pdf"><img src="../images/image.jpg" /></a>
<img src="../images/image1.jpg" />
<a href="www.site.com/document.txt"><IMG src="../images/image2.jpg" /></a>
<a href="www.site.com/document.doc"><img src="../images/image3.jpg" /></a>
<a href="www.site.com/document1.pdf">My PDF</a>
EOF;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$nodeList = $doc->getElementsByTagName('a');
for($i=0; $i < $nodeList->length; $i++) {
    $node = $nodeList->item($i);
    $children = $node->childNodes; 
    $hasImage = false;
    foreach ($children as $child) { 
       if ($child->nodeName == 'img') {
          $hasImage = true;
          break;
       }
    }
    if (!$hasImage)
       continue;
    if ($node->hasAttributes())
       foreach ($node->attributes as $attr) {
          $name = $attr->nodeName;
          $value = $attr->nodeValue;
          if ($attr->nodeName == 'href' && 
              preg_match('/\.(doc|pdf)$/i', $attr->nodeValue)) {
                echo $attr->nodeValue . 
                     " - Image is wrapped in a link to a PDF or DOC file\n";
                break;
          }

       }
}

现场演示:http: //ideone.com/dwJNAj

于 2013-04-18T11:19:56.217 回答