【问题标题】:How to alter anchor tags and image tags to add target = '_blank' attribute using Drupal 7 or PHP如何使用 Drupal 7 或 PHP 更改锚标签和图像标签以添加 target = '_blank' 属性
【发布时间】:2018-04-19 07:55:45
【问题描述】:

我想知道一种改变锚标签和图像标签以添加target='_blank'属性的方法。

我使用file_get_contents() 从外部源渲染内容并将其分配给一个变量。现在我想更改变量并将target='_blank' 属性分配给锚标记。

可以用 Drupal7 的方式还是 PHP 的方式?

<?php
$data=file_get_contents('https://example.com/live/widget/1');
?>

print_r($data);

<html>
<div>
  <span class="lw_item_thumb1">
      <a href="https://example.com/#!view/event/event_id/1">
        <img src="https://example.com/live/image/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg" alt="Floor" class="lw_image" width="80" height="80" srcset="https://example.com/live/image/scale/2x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 2x, https://example.com/live/image/scale/3x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 3x" data-max-w="3200" data-max-h="2400"/>
      </a>
    </span>
  <span class="lw_item_thumb2">
      <a href="https://example.com/#!view/event/event_id/2">
        <img src="https://example.com/live/image/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg" alt="Floor" class="lw_image" width="80" height="80" srcset="https://example.com/live/image/scale/2x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 2x, https://example.com/live/image/scale/3x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 3x" data-max-w="3200" data-max-h="2400"/>
      </a>
    </span>
</div>

</html>

我希望锚标签更改为

<a href="https://example.com/#!view/event/event_id/1" target='_blank'>

PS:我想避免使用 Javascript,而是想用 PHP 或 Drupal 的方式来做。

【问题讨论】:

    标签: php html drupal drupal-7


    【解决方案1】:

    您可以使用DOMDocument 解析HTML,使用getElementsByTagName() 查找&lt;a&gt; 标记,并使用setAttribute() 添加target 属性。

    在下面的代码中,我包含了 HTML 内联,当然,您可以使用 $data=file_get_contents('https://example.com/live/widget/1'); 代替 $data = '&lt;html&gt;....&lt;/html&gt;';。

    $data = '<html>
    <div>
      <span class="lw_item_thumb1">
          <a href="https://example.com/#!view/event/event_id/1">
            <img src="https://example.com/live/image/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg" alt="Floor" class="lw_image" width="80" height="80" srcset="https://example.com/live/image/scale/2x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 2x, https://example.com/live/image/scale/3x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 3x" data-max-w="3200" data-max-h="2400"/>
          </a>
        </span>
      <span class="lw_item_thumb2">
          <a href="https://example.com/#!view/event/event_id/2">
            <img src="https://example.com/live/image/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg" alt="Floor" class="lw_image" width="80" height="80" srcset="https://example.com/live/image/scale/2x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 2x, https://example.com/live/image/scale/3x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 3x" data-max-w="3200" data-max-h="2400"/>
          </a>
        </span>
    </div>
    
    </html>';
    
    $doc = new DOMDocument();
    // $doc->loadXML($data);
    $doc->loadHTML($data, LIBXML_NOWARNING); /* Thanks @NigelRen - see comments below */
    $elms = $doc->getElementsByTagName('a');
    foreach ($elms as $elm) {
        if (!$elm->hasAttribute('target')) {
            $elm->setAttribute('target','_blank');
        }
    }
    echo $doc->saveHTML();
    

    输出:

    <html>
    <div>
      <span class="lw_item_thumb1">
          <a href="https://example.com/#!view/event/event_id/1" target="_blank">
            <img src="https://example.com/live/image/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg" alt="Floor" class="lw_image" width="80" height="80" srcset="https://example.com/live/image/scale/2x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 2x, https://example.com/live/image/scale/3x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 3x" data-max-w="3200" data-max-h="2400">
          </a>
        </span>
      <span class="lw_item_thumb2">
          <a href="https://example.com/#!view/event/event_id/2" target="_blank">
            <img src="https://example.com/live/image/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg" alt="Floor" class="lw_image" width="80" height="80" srcset="https://example.com/live/image/scale/2x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 2x, https://example.com/live/image/scale/3x/gid/2/width/80/height/80/crop/1/src_region/0,0,3200,2400/example.rev.12345.jpg 3x" data-max-w="3200" data-max-h="2400">
          </a>
        </span>
    </div>
    
    </html>
    

    【讨论】:

    • 感谢您的回答,但我收到一个错误,即“警告:DOMDocument::loadXML(): Entity 中文档末尾的额外内容”,并且未显示数据。跨度>
    • 添加一个检查是否已经存在目标属性而不覆盖它的选项是否有用。
    • 将loadXML()更改为loadHTML()
    • 哦,对了@NigelRen。谢谢你。我用hasAttribute()添加了测试。
    • @NigelRen 更改后我收到此错误“警告:DOMDocument::loadHTML(): ID :ak.co 已在实体中定义”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多