0

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 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

Elitmiar
  • 35,072
  • 73
  • 180
  • 229
  • possible duplicate of [Best methods to parse HTML with PHP](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html-with-php) – Gordon Sep 01 '11 at 11:10
  • Also see http://stackoverflow.com/search?q=xpath+contains+php – Gordon Sep 01 '11 at 11:12
  • @Gordon - No duplicate, I do not need to know how to parse HTML, I know the above method is not the right one, but working on existing code that I can't just change. – Elitmiar Sep 01 '11 at 11:12
  • 1
    please define `I can't just change` ... (you are in the process to change it) – ajreal Sep 01 '11 at 11:13
  • @ajreal - Long story, but I understand your point – Elitmiar Sep 01 '11 at 11:16
  • 1
    convince your BOSS!!! (of course with a working solution) – ajreal Sep 01 '11 at 11:18
  • working example - http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element – ajreal Sep 01 '11 at 11:19
  • @Roland I admit that I was too lazy to find a more proper duplicate showing how to selectively replace a node value. I just needed a close reason and this is the most generic and always fits. Your question is basically the same as http://stackoverflow.com/questions/7268761/href-url-matching which was asked minutes ago. And be assured there is duplicates for that in the linked SO search. I just cannot be bothered to wade through them any more. Those parse HTML with Regex questions keep flowing in. It's just like fighting against windmills. – Gordon Sep 01 '11 at 11:25
  • Use Xpath: `/html/body//img[@src=not(contains(., 'maps.google.com'))]` – Gordon Sep 01 '11 at 11:27
  • Thx for all the comments, I think I will take the chance to rewrite it properly – Elitmiar Sep 01 '11 at 11:35

1 Answers1

1

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.

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • I changed the whole code, but I also tried your solution and your solution also did the job, I'll accept your answer – Elitmiar Sep 02 '11 at 08:01