Reputation: 50650
I'm looking for a way to find/replace links to images (within user-generated content) without touching links to non-images.
For example, the following text:
<a href="http://domain.com/arbitrary-file.jpg">Text</a>
<a href="http://domain.com/arbitrary-file.jpeg">Text</a>
<a href="http://domain.com/arbitrary-path/arbitrary-file.gif">Text</a>
<a href="http://domain.com/arbitrary-file.png">Text</a>
<a href="http://domain.com/arbitrary-file.html">Text</a>
<a href="http://domain.com/arbitrary-path/">Text</a>
<a href="http://domain.com/arbitrary-file#anchor_to_here">Text</a>
Non-hyperlinked URL: http://domain.com/arbitrary-path/arbitrary-file.gif
Non-hyperlinked URL: http://domain.com/arbitrary-file#anchor_to_here
... should be revised to:
<img src="http://domain.com/image.jpg" alt="Text" />
<img src="http://domain.com/arbitrary-file.jpeg" alt="Text" />
<img src="http://domain.com/arbitrary-path/arbitrary-file.gif" alt="Text" />
<img src="http://domain.com/arbitrary-file.png" alt="Text" />
<a href="http://domain.com/arbitrary-file.html">Text</a>
<a href="http://domain.com/arbitrary-path/">Text</a>
<a href="http://domain.com/arbitrary-file.html#anchor_to_here">Text</a>
Non-hyperlinked URL: http://domain.com/arbitrary-path/arbitrary-file.gif
Non-hyperlinked URL: http://domain.com/arbitrary-file#anchor_to_here
... securely and reliably in PHP.
Upvotes: 3
Views: 2607
Reputation: 6550
You might want to look at using a HTML parser (rather than regular expressions, as you tagged the submission) such as the PHP Simple HTML DOM Parser. This would provide you with the reliability you speak of.
You'll probably end up with something like this:
foreach($html->find('a') as $element)
{
echo '<img src="'.$element->href.'" alt="'.$element->innertext.'" />';
}
Upvotes: 7
Reputation: 124768
There's no reliable way to do this, not at least with regular expressions, but this should do the trick nevertheless:
$str = preg_replace('~<a[^>]*?href="(.*?(gif|jpeg|jpg|png))".*?</a>~', '<img src="$1" />', $str);
To open this up a bit:
<a
tags"
character</a>
tag in the replaceAs Bauer noted, you could be better off using DOM methods. But if you can be sure your links are always in this format, you can use regular expressions. Regex might be a bit faster also.
Upvotes: 4