Page 1 of 1

Eh!regi_replace()

Posted: Sat Dec 06, 2003 11:10 am
by Gen-ik
Would someone be kind enough to explain why the following ereg isn't working. I'm too hot on the whole ereg thing yet but most of my other stuff is working... it's just this which is being a pain in the rear.

Code: Select all

<?php

$string = "Hello. This is me.. [image=http://www.site.com/image.jpg] whoo!";

$f = "\\[image=([^\\]]+)\\]";
$t = "<img src="\\1" alt="" />";

$string = eregi_replace($f, $t, $string);

echo $string;

// Should echo().....
//
// Hello. This is me.. <img src="http://www.site.com/image.jpg" alt="" /> whoo!
//
// .....but doesn't

?>
I think it's a problem with the square brackets, PHP isn't spitting out any errors which I guess means that the 'search pattern' is wrong.

Thanks.

Posted: Sat Dec 06, 2003 11:31 am
by Weirdan

Code: Select all

$f = "\\[image=([^]]+)\\]";

Code: Select all

$ man 3 regexp
................
To include a literal `]' in the sequence, make it the first character
(following a possible `^').  To include a literal `-', make it the first 
or last character.
................
You don't need to escape closing square bracket in the range.

Posted: Sat Dec 06, 2003 12:23 pm
by Gen-ik
Ah I see. Ok, thanks for your help.. all seems to be working now.. which is nice :)