php - 将图像 url 替换为 img 标签

标签 php html

我正在尝试用 img 标签替换字符串中的 URL。

我可以正常工作,但是它还替换了 anchor 标记内的 URL。

如果我有:

 $string = 'Hi, https://example.com/logo.png,
     <a href="https://example.com/banner.png">https://example.com/banner.png</a>';

它看起来像:

Hi, <img src="https://example.com/logo.png" />,
     <a href="<img src="https://example.com/banner.png" />"><img src="https://example.com/banner.png" /></a>

这就是我用来替换的:

return preg_replace_callback('/https?:\/\/(.*?)\.(jpg|png|gif)(\?\w+=\w+)?/i',    function($matches){ 
        return '<img src="'.$matches[0].'" />'; 
}, $string);

如何让它忽略 anchor 标记。

谢谢。

最佳答案

不要直接使用正则表达式进行 HTML 操作。相反,您可以使用 DOMDocument 来进行 DOM 操作。查找 HTML 字符串中的所有文本节点,并安全地将图像 URL 替换为图像标签。

<?php

$string = 'Hi, https://example.com/logo.png,
<a href="https://example.com/banner.png">https://example.com/banner.png</a>';

$dom = new DOMDocument();

// This loads the HTML string in a special way to handle utf and 
// not append any extra HTML tags
$dom->loadHtml(mb_convert_encoding($string, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

// Get the text nodes
$xpath = new DOMXPath($dom);
foreach($xpath->query('//text()') as $node) {
    // Replace the text nodes with the replaced HTML fragments
    $replaced = preg_replace('/(https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif))/', '<img src="$1" alt="$1" />', $node->data);
    $frag = $dom->createDocumentFragment();
    $frag->appendXML($replaced);
    $node->parentNode->replaceChild($frag, $node);
}

echo $dom->saveHtml();

输出:

<p>Hi, <img src="https://example.com/logo.png" alt="https://example.com/logo.png">,
<a href="https://example.com/banner.png"><img src="https://example.com/banner.png" alt="https://example.com/banner.png"></a></p>

如果您还想排除 anchor 之间的图像 URL,则可以使用

$xpath->query('//text()[not(ancestor::a)]');

输出:

<p>Hi, <img src="https://example.com/logo.png" alt="https://example.com/logo.png">,
<a href="https://example.com/banner.png">https://example.com/banner.png</a></p>

我在演示中使用了这个正则表达式。请根据您的目的进行修改。

preg_replace('/(https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif))/', '<img src="$1" alt="$1" />', $string);

关于php - 将图像 url 替换为 img 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440087/

相关文章:

php - 是否可以在一页上显示多个 jQuery Fancybox 警报?

php剥离html标签及其数据并插入到mysql中

javascript - 在 JQuery 或 Javascript 中 append HTML 表单

java - selectitem 和 selectitems 标签的区别

javascript - 我如何使 QuerySelectorAll() 选择所有 html 标签并通过访问 html 标签的索引进行更改

php - 从多个表中获取对象的最佳方法是什么?

php - fatal error :消息 'Zend_Gdata_App_HttpException'未被捕获的异常 'Expected response code 200, got 401'

javascript - 如何将我的代码从 div 标签更改为 textarea?

PHP ftp_put - 找不到文件或无法访问

javascript - ngf-max-size $digest() 与 angular.js 中的 ng-file-upload 错误