Hi, im making a bbcode editor and am using the below code to replace the tags entered. the values come from a database where i store them as:
id, name, code, type, replacement
where a standard bold would be:
#, bold, b, 1, <strong>$1</strong>
and a double url would be:
#, url, url, 2, <a href='$1'>$2</a>
the type defines wether there are two variables in the replacement. in the code below single variable replacements work but doubles don't. when using links the variable $1 is repeated for both (e.g.: <a href='$1'>$1</a>). any ideas why?
function formatbb($bbcontent) {
$bbcontent = str_replace('<', '<', $bbcontent);
$bbcontent = str_replace('>', '>', $bbcontent);
$bbcontent = str_replace('<br />', "\n", $bbcontent);
$sql = mysql_query("SELECT * FROM bbcodes ORDER BY id ASC") or die("Cannot get codes: ".mysql_error());
$i = 0;
while($patterns = mysql_fetch_array($sql)) {
if($patterns['type'] == '1') {
$pattern[$i] = "|\[".$patterns['code']."\](.*?)\[/".$patterns['code']."\]|s";
$replacement[$i] = $patterns['replacement'];
} else {
$pattern[$i] = "|\[".$patterns['code']."=(.*?)\](.*?)\[/".$patterns['code']."\]|s";
echo $pattern[$i]."<br />";
$replacement[$i] = $patterns['replacement'];
}
$i++;
}
$bbcontent = preg_replace($pattern, $replacement, $bbcontent);
return $bbcontent;
}