RickyAYoder RickyAYoder - 4 years ago 88
PHP Question

How to make PHP BB Codes with RegExp?

for my website, I want a simple BB code system. Nothing special--just hyperlinks and images will be fine for now.

I'm not good with RegExp. Period. But if someone could show me an example, I may be able to grasp it to clone it into different tags.

Your help is very appreciated!

Answer Source

The user asked for something simple, so I gave him something simple.

$input = "[link=http://www.google.com]test[/link]";
$replacement = preg_replace('/\[link=(.*?)\](.*?)\[\/link\]/', '<a href="$1">$2</a>', $input);

Where /\[link=(.*?)\](.*?)\[\/link\]/ is the regex, <a href="$1">$2</a> is the format, $input is the input/data, and $replacement is the return.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download