PHP正则表达式精确url匹配,更改img src

发布于 2024-11-30 07:23:29 字数 960 浏览 1 评论 0 原文

我有一个 HTML 文档,标签中包含一些图像。我想用不同的、在数组中定义的值更改每个 img 源(src)。

当我运行以下代码时,我得到了

Warning: preg_replace(): Unknown modifier '/' in /var/www/test.php on line 9
Warning: preg_replace(): Unknown modifier '/' in /var/www/test.php on line 9

How do I escape the url strings permanent?

<?php
$body = "<img src=http://www.google.com/img1.jpg><br>"
      + "<img width=300 src=http://www.bbc.co.uk/img1.jpg>";

$changes = array();
$changes[] = array('from' => 'http://www.google.com/img1.jpg',
                     'to' => 'http://cnn.com/image2.jpg');
$changes[] = array('from' => 'http://www.bbc.co.uk/img1.jpg',
                     'to' => 'http://www.foo.bar.com/image2.jpg');

foreach ($changes as $change){
    $body = preg_replace('/<(img.*src)="'.$change['from'].'".*\/?>/',
                         '<\1="'.$change['to'].'">', $body)
            . PHP_EOL;
}
?>

I have a HTML document with some images in tags. I would like to change each img sources (src) with different, defined in array, value.

When I run the following code I get

Warning: preg_replace(): Unknown modifier '/' in /var/www/test.php on line 9
Warning: preg_replace(): Unknown modifier '/' in /var/www/test.php on line 9

How do I escape the url strings correctly?

<?php
$body = "<img src=http://www.google.com/img1.jpg><br>"
      + "<img width=300 src=http://www.bbc.co.uk/img1.jpg>";

$changes = array();
$changes[] = array('from' => 'http://www.google.com/img1.jpg',
                     'to' => 'http://cnn.com/image2.jpg');
$changes[] = array('from' => 'http://www.bbc.co.uk/img1.jpg',
                     'to' => 'http://www.foo.bar.com/image2.jpg');

foreach ($changes as $change){
    $body = preg_replace('/<(img.*src)="'.$change['from'].'".*\/?>/',
                         '<\1="'.$change['to'].'">', $body)
            . PHP_EOL;
}
?>

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

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

发布评论

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

评论(1

狂之美人 2024-12-07 07:23:29

试试这个:

http://php.net/manual/en/function.urlencode.php< /a> 用于编码 url。

但是,看起来您只是想替换 $body 中的字符串的一部分,所以我会使用

http://php.net/manual/en/function.str-replace.php

而不是您的正则表达式。

<?php
$body = "<img src=http://www.google.com/img1.jpg><br><img width=300 src=http://www.bbc.co.uk/img1.jpg>";

$changes = array();
$changes[] = array('from' => 'http://www.google.com/img1.jpg', 'to'=> 'http://cnn.com/image2.jpg');
$changes[] = array('from' => 'http://www.bbc.co.uk/img1.jpg', 'to' => 'http://www.foo.bar.com/image2.jpg');

foreach ($changes as $change){
$body = str_replace($change['from'],$change['to'],$body);
}
?>

Try this:

http://php.net/manual/en/function.urlencode.php for encoding urls.

However, it looks like you're just trying to replace a part of the string in $body, so I'd use

http://php.net/manual/en/function.str-replace.php

instead of your regular expression.

<?php
$body = "<img src=http://www.google.com/img1.jpg><br><img width=300 src=http://www.bbc.co.uk/img1.jpg>";

$changes = array();
$changes[] = array('from' => 'http://www.google.com/img1.jpg', 'to'=> 'http://cnn.com/image2.jpg');
$changes[] = array('from' => 'http://www.bbc.co.uk/img1.jpg', 'to' => 'http://www.foo.bar.com/image2.jpg');

foreach ($changes as $change){
$body = str_replace($change['from'],$change['to'],$body);
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文