开发者

Replacing image source in HTML image tag, and excludes certain images

I have the following code

$body = preg_replace('/<img(.*?)src="(.+)"/Ui','<img src="renderImage.php?image=\\2&width=<?=$img_width?>&url=<?=$mobiURL?>"',$body);

$body contains a whole html page that contains 开发者_C百科img tags, the above code works perfectly, but I need to exclude images with the string maps.google in it so that it does not get replaced.

Is there any way to do this


Method#1 : Instead of altering your regular expression and making it more complex. You can use preg_replace_callback instead.

In your callback function, you can ignore maps.google.com images.

http://php.net/manual/en/function.preg-replace-callback.php

Method#2 :

  1. Invalidate IMG tages with maps.google.com src first. eg. convert a <IMG SRC='http://maps.google.com.... tag to

    <TEMPORARY_TAG src='http://maps.google.com... using regular expression.

  2. Run your original regular expression to replace all IMG tags with your required snippet, that would not replace the ones with maps.google.com src as we had altered that IMG tag to TEMPORARY_TAG in step#1

  3. Do reverse of step#1 , replace TEMPORARY_TAG in your HTML with IMG using simple string replace or regex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜