Suppose I have a Markdown string:
* Bullet has *bold [code]*test[/code] part*. *Another\nbold* item.
And I want to convert it to BBCode like:
* Bullet has [b]bold [code]*test[/code] part[/b]. [b]Another\nbold[/b] item.
…and, as you can tell above, preserve what was inside [code]. How would I accomplish this?
BACKGROUND
See, I’m trying to tweak a FluxBB forum. It permits BBCode by default. However, I wanted to also permit a small subset of Markdown for noobs, such as bolding and italics. At least for bolding, this is possible with a statement like:
$out = preg_replace('/\*(\S.*?\S)\*/s','[b]$1[/b]',$in);
…but has a problem with the *test part, where it wants to translate that too.
The following code seems to work. I preserve using a
preg_replace_callback()andbin2hex(), and then unhex viapack()through anotherpreg_replace_callback().