如何使用 Drupal 7 或 PHP 更改锚标记和图像标记以添加 target = '_blank' 属性

How to alter anchor tags and image tags to add target = '_blank' attribute using Drupal 7 or PHP

我想知道一种更改锚标记和图像标记以添加 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 方式。

您可以使用 DOMDocument to parse the HTML, getElementsByTagName() to find <a> tags, and setAttribute() 添加 target 属性。

在下面的代码中,我包含了 HTML 内联,但是,当然,您可以使用 $data=file_get_contents('https://example.com/live/widget/1'); 而不是 $data = '<html>....</html>';

$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>