如何修改锚标签和图片标签以添加目标=' _blank'使用Drupal 7或PHP

时间:2018-04-19 07:55:45

标签: php html drupal drupal-7

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

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

是否可以用Drupal7方式或PHP方式进行?

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

的print_r($数据);

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

我想将锚标记更改为

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

PS:我想避免使用Javascript,并希望使用PHP或Drupal方式。

1 个答案:

答案 0 :(得分:3)

您可以使用DOMDocument来解析HTML,getElementsByTagName()来查找<a>代码,使用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>