Eh!regi_replace()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Eh!regi_replace()

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Ah I see. Ok, thanks for your help.. all seems to be working now.. which is nice :)
Post Reply