I'm trying to implement a BBCode style set of tags for one of my scripts, but I can't get it to replace things properly, here's the code I use:
$test = "[link=\"http://www.april-showers.com\"]APRIL SHOWERS[/link][newline]";
$test .= "[link=\"http://www.pie.com\"]PIE.COM[/link][newline]";
$test .= "[img=\"groovy.gif\"]Groovy[/img]";
$patterns[0] = "/\[link=\"(.*)\"\]/";
$patterns[1] = "/\[\/link\]/";
$patterns[2] = "/\[img=\"(.*)\"\](.*)/";
$patterns[3] = "/\[\/img\]/";
$patterns[4] = "/\[newline\]/";
$replacements[0] = "<a href=\"\\1\">";
$replacements[1] = "</a>\n";
$replacements[2] = "<img src=\"\\1\" alt=\"\\2\">";
$replacements[3] = "</img>\n";
$replacements[4] = "\n<br>\n";
$output = preg_replace( $patterns, $replacements, $test);
echo $output;
Obviously the $test variable is there to test I got the code right, as far as I can tell the [newline] tag works, the [/img] and [/link] tags work, but the others go horribly wrong.
Any help would be appreciated. 🙂