lesmith
11-09-2007, 12:48 PM
Hello Peeps
I am not good at all with preg_replace and would hopefully someone can advice.
How can I strip out a img tag and just get it's alt value ie.
Replace this:
<img src="/img/image.gif" alt="This Image" />
with this
"This Image"
I hope this makes sense.
hope you can advise.
thanks in advanced if you can.
Lee
shyam
11-09-2007, 02:07 PM
preg_replace('/<img .*?\salt="([^"]+)"[^>]*>/i', '$1')
lesmith
11-09-2007, 02:15 PM
Thanks for the reply but I cant get it to work.
Here is my exmaple of yours.
$1 = '<img src="/img/test.gif" alt="m test" />';
echo preg_replace('/<img .*?\salt="([^"]+)"[^>]*>/i', '$1');
Am I doing it wrong ?
Thanks Again
Lee
shyam
11-09-2007, 02:19 PM
thats gotta be
$text = '<img src="/img/test.gif" alt="m test" />';
echo preg_replace('/<img .*?\salt="([^"]+)"[^>]*>/i', '$1', $text);
by way of explanation, the '$1' is a reference to the first (in this case only) ()'d bit inside the pattern. The manual page (http://php.net/preg-replace) has more, see the bit on 'replacement' and the first example.
lesmith
11-09-2007, 02:36 PM
Thats Fantastic work really well.
could I ask one more thing. could I add brackets around the name like.
[img name]
Just so it makes it easier to see.
Hope you can advice.
Thank you very much.
Regards
Lee
lesmith
11-09-2007, 03:14 PM
Dont worry I was being thick.
Got it working perfect.
Thank you very much.
regards
Lee