tags:

views:

697

answers:

3

[Edited - Sorry Bart] I've looked at other answers but struggling to match this. I want to wrap an image tag where the src is the second attribute (after title) with a specific anchor tag that contains a link to the image found in the src from the image tag.

Example of img tag in string. This has been entered via tinymce wysiwyg and always adds title then src.

<img title="who_main_Layer_1.jpg" src="../../images/who_main_Layer_1.jpg" alt="who_main_Layer_1.jpg" width="380" height="268" />

I need to take all of these and wrap with the following href:

<a href="event:images/expand/image.jpg"><img src=”images/image.jpg” /></a>

The image src points to the thumbnail and the (Flash AS3 Event) pops up the full size version. Both images named the same just different folders.

Here is a full example of a string that would need the regex running against (Due to sensitive data I've substituted text for Lorem ipsum, but the layout is the same!):

<p>Lorem ipsum dolor sit amet</p>
<p>&nbsp;</p>
<p>Lorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit  
ametLoremipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem 
ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit amet</p>
<p>&nbsp;</p>
<p><img title="who_main_Layer_1.jpg" src="../../images/who_main_Layer_1.jpg" 
alt="who_main_Layer_1.jpg" width="380" height="268" /></p>
<p>&nbsp;</p>
<p>Lorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem 
ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum 
dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor 
sit  
ametLorem ipsum dolor sit ametLorem ipsum dolor sit amet</p>
<p>&nbsp;</p>
<p><img title="who_main_Layer_1.jpg" src="../../images/who_main_Layer_1.jpg" 
alt="who_main_Layer_1.jpg" width="380" height="268" /></p>`

Many thanks in advance, Marc

+2  A: 

Similar questions have been answered several times and the answer is always the same: do not use regular expressions to tamper with HTML. In PHP, you can use XPath and the SimpleXml or DOMParser extensions to solve this problem.

Sorry for posting so many links to my own answers but the answers themselves and the questions they are answering contain a lot of information on the subject.

soulmerge
Thanks, I'll look at those links. Starting with 'do not use regex' :) I was sure you could use it; but will read now. Cheers.
ed103
You can use it, but it quickly gets very tricky, because what you are manipulating is a grammar which (mostly) takes note of structure but ignores layout, whereas regexp is really a character-by-character mechanism.
Colin Fine
+1  A: 

Try this code:

<?php
$str = '<img title="who_main_Layer_1.jpg" src="../../images/who_main_Layer_1.jpg" alt="who_main_Layer_1.jpg" width="380" height="268" />';

preg_match('#src="(?:.*/)?(.*?)"#', $str, $match);
$src = $match[1];
?>
<a href="event:images/expand/<?php echo $src; ?>"><img src=”images/<?php echo $src; ?>” /></a>

EDIT: another version to account for multiple tags in the string:

$replace = '<a href="event:images/expand/$1"><img src="images/$1" /></a>';
$str = preg_replace('#<\s*img.*?src="(?:[^"]+/)?(.*?)".*?>#s', $replace, $str);
kemp
That works perfectly for that string. Thanks. It doesn't work where the example tag is embedded in another string, and possibly multiple tags? How do I take your example and replace back into the original string? Cheers
ed103
Also provide examples of those cases. This is the risk of just providing one example.
Bart Kiers
Edited my answer with a version for multiple tags, see if it works for you.
kemp
Bart, addded full example to original question text.
ed103
Kemper, Thanks again. Testing now.
ed103
Second edit, forgot /s modifier to the regexp -- seems to work with the provided text
kemp
Spot on!!!! Thanks Kemper. If I could, I'd buy you a beer! Cheers!
ed103
A: 

Try this :

$newString = preg_replace('`<img([^>]*)src="\\.\\./\\.\\./images/([^"]+)"([^>])*>`','<a href="event:images/expand/$2"><img$1src="images/$2"$3></a>', $oldString);

Limitations are :

  • It will apply the changes in things like <input value='<img src="../../images/test.jpg/>"'/>
  • If " are replaced by ' in your img tags, you'll have to change the regexp
  • It will choke on things like <img alt="6>5" src="../../images/test.png"/>

I agree with other commenters saying regexp are bad to parse HTML. But there's almost no parsing here and the format of things to replace seems to be under control (generated by tinymce).

Arkh
Thanks Arkh. Kempers solution above seems to have done the trick.
ed103