Shoutbox

Preg replace syntax - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Preg replace syntax (/showthread.php?tid=87614)

Preg replace syntax by bramvandeperre on 12-05-2008 at 10:54 PM

Heya all!

I got some little question for you.

code:
<img src="image.png" alt="blah" /> this is an image.


should become
code:
<a href="image.png">blah</a> this is an image.


I wanted to do this using a preg replace, but I suck in writing patterns.

Can anyone help? make sure that images are changed into hyperlinks... if there's no alt, the filename should be the text (so not only preg_replace but also some if/else thingies :P

thanks in advance

Bram

oh almost forgot: it's PHP...
RE: Preg replace syntax by Ezra on 12-05-2008 at 11:31 PM

Little regex without the alt thingy you wanted, i'm sure you or someone else can make that work for you

Regex:

code:
<img src=\"(.*)"\salt=\"(.*)" />(.*)
Replacement string:
code:
<a href=\"\1\">\2</a>\3

RE: RE: Preg replace syntax by bramvandeperre on 12-05-2008 at 11:40 PM

quote:
Originally posted by Ezra
Little regex without the alt thingy you wanted, i'm sure you or someone else can make that work for you

Regex:
code:
<img src=\"(.*)"\salt=\"(.*)" />(.*)
Replacement string:
code:
<a href=\"\1\">\2</a>\3


thanks alot, but what if there is no alt at all? then the filename should be there (like image001.jpg or so)