• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Regex/pregmatch question?


Well-known member
Can anyone help me nail down the syntax for a preg_match to pull the BBCode out of the raw post content in the DB field?

It's gotta be pretty close to:

preg_match('/\[img(=(.*?))?\](.*?)\[\/img\]/', $subject, $results);

At least I *think* so, I'm not even vaguely a regexian ;).

Any help would be greatly appreciated!



Well-known member

Wait... Suddenly remembered that Jaxel is doing this in XenPorta.

preg_match('#\[img\](.+?)\[/img\]#i', $post['message'], $matches)

Aha! :) Thx Jaxel!



Active member
Could I use this to replace multiple line breaks with a single line break?

I'm thinking the match pattern would be something like:


And the replacement would be


Would this work?


Well-known member
I would use this:
$post['message'] = preg_replace('#\n{2,}#', "\n", trim($post['message']));
This would search for instances of 2 or more \n tags and replace it with \n... you could also use \r\n if you needed it, but on my forum \n works better.