0

I have a PHP page which looks through some HTML for links and replaces them with links to a local PHP page; the problem is finding image links. I currently use this code:

$data = preg_replace('|(<a\s*[^>]*href=[\'"]?)|','\1newjs.php?url=', $data);

Which matches things like

<a href="http://google.com">Google</a>

and will replace them with

<a href="newjs.php?url=http://google.com">Google</a>

I am looking to do a similar things with image files (jpg, gif, png) and replace something like:

<a href="http://google.com/hello.png">Image</a>

With this:

<a href="newjs.php?url=http://google.com/hello.png&image=1">Image</a>

Note the '&image=1' in the new URL. Is it possible for me to do this using PHP, preferably with regular expressions?

q3d
  • 3,473
  • 8
  • 34
  • 39

1 Answers1

1

As per usual with anything involving regexes and HTML: https://stackoverflow.com/a/1732454/118068

The proper solution is to use DOM operations:

$dom = new DOMDocument();
$dom->loadHTML(...);
$xp = new DOMXPath($dom);
$anchors = $xp->query('//a');
foreach($anchors as $a) {
   $href = $a->getAttribute('href');
   if (is_image_link($href)) { // 
       $a->setAttribute('href', ... new link here ...);
   }
}
Community
  • 1
  • 1
Marc B
  • 356,200
  • 43
  • 426
  • 500