I'm having a huge problem with preg_replace and backreferences:
$name in the function leftimage is not what I would expect, e.g.
if $story ="foo [leftimage]front[/leftimage] bar";
then I would expect $name in the function leftimage to be "front" but it is "$1" instead, and hence the database query does not work.
So my question is how do you pass the backreference from the regexp to a function so that it can be used?
Thanks.
The code:
class parser {
function parsestory ($story,$id) {
$patterns = array (
"/\[b\]([ \w]+)\[\/b\]/i", # bold
"/\[i\]([ \w]+)\[\/i\]/i", # italic
"/\n/", # newline
"/\[leftimage\]([ \w]+)\[\/leftimage\]/" # leftimage
);
$replace = array (
"<b>\\1</b>", # bold
"<i>\\1</i>", # italic
"<br>", # newline
$this->leftimage('$1',$id) # leftimage
);
$story = preg_replace ($patterns, $replace, $story);
$story = stripslashes($story);
return $story;
}
function leftimage ($name,$id) {
$this->database->db_qry("SELECT id FROM images WHERE module='story' AND imageid=".$id." AND name='".$name."'", $full);
$image = $this->database->get_data();
return "<img src=\"".$this->html->index("script=image&id=".$image[0])."\" align=left>";
}
}