since I use a CSS hack to add a small icon to external links (a[href^=”http:”]), I have to add a class to each image in my post (img class=”imagelink”) with a “no icon”-style. It simply sets the background back from the icon to “transparent”. Otherwise, I’d have this icon even on images that link to external sites, which looks ugly and which I don’t want.
Here is what I want to achieve:
I want to hook into
add_filter(). The function hooking in should use regular expressions to find all image tags that are surrounded by link tags. Then,
class="imagelink"should automatically be added to the img-tag. Therefore, I do not need to pay attention to add an “imagelink”-class every time I link an image to an external website.
Here is an example:
<a href="http://link.to.external.site" target="_blank"><img src="image.used.for.link" /></a>
<a href="http://link.to.external.site" target="_blank"><img class="imagelink" src="image.used.for.link" /></a>
I think that
preg_replace()can be used for this, but I have absolutely no idea how I should construct the pattern and the replacement string, since I have no great experience with RegEx.
The other problem is that I really only want to add the
class="imagelink"without modifying any of the other parameters of the link- or the img-tag.
I’ve tried this with a (German) RegEx Tester and it seems to be get close:
$pattern = ‘/<a(.*)><img src=(.*)><\/a>/’;
The problem is the greediness of
(*.), since I do not want a lot of code to be matched by that pattern.
In addition to this, I have absolutely NO IDEA what the replacement should look like. :’-(
I’d be grateful for some help on this.
- The topic ‘Help on RegEx needed (for the_content filter)’ is closed to new replies.