替换所有的src属性值HTML 文档中的标签

发布于 2024-10-06 05:44:16 字数 593 浏览 4 评论 0原文

我有以下代码(php),它将匹配 img-src 并替换为新的 url

$rep = array('/', '+', '(', ')');
$with = array('\/', '\+', '\(', '\)');

$match_pattern = '/<img[^<]*src\s*=\s*\"'.str_replace($rep, $with, $source_url).'\"[^>]*>/iUu';
$img_replace_str = '<img src="'.$new_url.'" />';
$post_content = preg_replace($match_pattern, $img_replace_str, $post_content);

对于 srchttp://www.example.com/a.jpg,没有问题,但对于 src 包含查询字符串(如 http://www.example.com/b.jpg?height=900)的图像,不匹配。

我想匹配带有和不带有查询字符串的图像标签。

I have following code (php), it will match img-src and replace with new url

$rep = array('/', '+', '(', ')');
$with = array('\/', '\+', '\(', '\)');

$match_pattern = '/<img[^<]*src\s*=\s*\"'.str_replace($rep, $with, $source_url).'\"[^>]*>/iUu';
$img_replace_str = '<img src="'.$new_url.'" />';
$post_content = preg_replace($match_pattern, $img_replace_str, $post_content);

For images that have src as http://www.example.com/a.jpg, there is no issue, but for images that have src that contains query string like http://www.example.com/b.jpg?height=900, it's not matching.

I want to match image tags with and without a query string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2024-10-13 05:44:16

您可以使用 PHP 的 preg_quote()-function 代替 str_replace()< /代码>。它会自动转义所有正则表达式特殊字符(请参阅文档)。这应该可以解决问题,因为您的 str_replace() 解决方案没有转义 ?,它是正则表达式中的特殊字符:

$match_pattern = '/<img[^<]*src\s*=\s*\"'.preg_quote($source_url, '/').'\"[^>]*>/iUu';

You can use PHP's preg_quote()-function instead of str_replace(). It automatically escapes all regular expression special characters (see the docs). That should solve the problem, since your str_replace()-solution did not escape ?, which is a special character in regular expressions:

$match_pattern = '/<img[^<]*src\s*=\s*\"'.preg_quote($source_url, '/').'\"[^>]*>/iUu';
鹊巢 2024-10-13 05:44:16

使用合法的 DOM 解析器可以轻松直观地替换包含任何属性的 标签的 src 属性值。 XPath 仅针对 标记的 src 属性进行了非常直接的工作。

代码:(演示

$html = <<<HTML
<div>
Here is an img tag with no qs <img src="http://www.example.com/a.jpg">,
 an img with no src <img title="to be determined">,
 and here is another with a qs <img src="http://www.example.com/b.jpg?height=900">.
Here is a <iframe src="http://www.example.com/c.jpg?foo=bar"></iframe> and
 a submit button <input type="image" src="http://www.example.com/d.jpg?boo=far&what=now" alt="Submit">
</div>
HTML;

$newUrl = 'https://www.example.com/new.jpg';

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
foreach ($xpath->query("//img/@src") as $src) {
    $src->value = $newUrl;
}

echo $dom->saveHTML();

输出(经过两次合格的替换后):

<div>
Here is an img tag with no qs <img src="https://www.example.com/new.jpg">,
 an img with no src <img title="to be determined">,
 and here is another with a qs <img src="https://www.example.com/new.jpg">.
Here is a <iframe src="http://www.example.com/c.jpg?foo=bar"></iframe> and
 a submit button <input type="image" src="http://www.example.com/d.jpg?boo=far&what=now" alt="Submit">
</div>

Use a legitimate DOM parser to easily and intuitively replace src attribute values of <img> tags containing any manner of attributes. XPath does an exquisitely direct job of targeting the src attribute of <img> tags ONLY.

Code: (Demo)

$html = <<<HTML
<div>
Here is an img tag with no qs <img src="http://www.example.com/a.jpg">,
 an img with no src <img title="to be determined">,
 and here is another with a qs <img src="http://www.example.com/b.jpg?height=900">.
Here is a <iframe src="http://www.example.com/c.jpg?foo=bar"></iframe> and
 a submit button <input type="image" src="http://www.example.com/d.jpg?boo=far&what=now" alt="Submit">
</div>
HTML;

$newUrl = 'https://www.example.com/new.jpg';

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
foreach ($xpath->query("//img/@src") as $src) {
    $src->value = $newUrl;
}

echo $dom->saveHTML();

Output (after two qualifying replacements):

<div>
Here is an img tag with no qs <img src="https://www.example.com/new.jpg">,
 an img with no src <img title="to be determined">,
 and here is another with a qs <img src="https://www.example.com/new.jpg">.
Here is a <iframe src="http://www.example.com/c.jpg?foo=bar"></iframe> and
 a submit button <input type="image" src="http://www.example.com/d.jpg?boo=far&what=now" alt="Submit">
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文