I've been trying to make a mini markup parser similar to Markdown in PHP. The input looks like this:
Some text **that is bold!** and some other text
into
Some text <b>that is bold!</b> and some other text
I've found countless resources on how to replace a chunk of text with another, but only one that actually replaces suffix and postfix: this answer. After messing around with it a bit it looks like this:
$s=preg_quote("**");
$in = preg_replace(
array('%'.$s.'-%', '%-%', '%'.$s.'%'),
array('<b>','_','</b>'),
$in);
Earlier attempts are equally clumsy. It's obvious I don't really get regex or that engine's syntax so I'm at a loss. The alternative is to hack the string into pieces and add/remove what I need, but regex seems much more elegant. Any ideas?
Fix: Earlier I wrote $s=preg_quote($s)
by mistake, corrected to $s=preg_quote("**")