Page 1 of 1

Interpreting text in PHP

Posted: Sun Jul 24, 2005 5:02 am
by srg_13
Hi,

How can I write a php script, so if in a varable there is the text "[link=http://www.foo.com]Stuff[/link]" (without quotes), it just displays "[link]"??

These tags would be inside lots of other plain text.

Thanks in advance,

-Stephen

Re:Interpreting text in PHP

Posted: Sun Jul 24, 2005 6:39 am
by AGI1122
You can do this with preg_replace.

Code: Select all

$text = '[link=http://www.foo.com]Stuff[/link]';
$text = preg_replace('/\[link=(.+?)\](.+?)\[\/link\]/is','<a href="\\1">\\2</a>',$text);
As you can see it will replace it with an actual link. You can adjust it to do exactly what you want.

Re:Interpreting text in PHP

Posted: Sun Jul 24, 2005 8:22 am
by Warrior
Adittionally you might want to use an array to match each replacement to what they are replaced with and use array_keys() and array_values().

This is of course if you plan to have a lot of BBCode and dont want to have a replacement for everyone.