Interpreting text in PHP

Programming, for all ages and all languages.
Post Reply
srg_13

Interpreting text in PHP

Post 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
AGI1122

Re:Interpreting text in PHP

Post 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.
Warrior

Re:Interpreting text in PHP

Post 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.
Post Reply