Php – preg_replace() not finding ending delimiter

MySQLPHPpreg-matchpreg-replace

I use preg_replace() alot but I'm not a genius at it.

If I start up a function and deliberately key in all the smilies that I want to use e.g.

<?php
function parse_emotes($body){
    $body = preg_replace("#\:p#i", "<img src='images/emotes/tongue.png' alt=':p' title=':p' />", $body);
    //they continue like i said
    return $body;
}
?>

but today I tried to change it up and use mysql to let me just insert and delete them as I please without playing in my code, but when I tried it, it only throws

Warning: preg_replace() [function.preg-replace]: No ending delimiter
'#' found in PATH/TO/FILE.php on line 226

Here is the code I used the first time:

<?php
function parse_emotes($body){
    while($row = $db->Query("SELECT * FROM emotes", 3)) {

        return $body = preg_replace($row['regex'], "<img src='images/emotes/" . $row['src'] . "' alt='" . $row['alt'] . "' title='" . $row['alt'] . "' />", $body);
    }
}
?>

That didn't work, and yes the regex row included the delimiters so it would output #\:p#

I got the same error as stated before and then I tried to take the #s out of the data in MySQL and just changed preg_replace as follows

preg_replace('#' . $row['regex'] . '#i', .......)

and it still does not like it? I have also tried assigning:

$regex = $row['regex'];
preg_replace("#$regex#i");

Guess what? Still a no. Any help with where to go with this is very appreciated.

Best Answer

Since people are still downvoting this topic. @salathe was correct in the questions comments (returning in the loop.. Ooops).

but here is the answer:

$emotes = $db->select(['regex', 'class'])->from("emotes")->execute();
while ($emote = $db->fassoc($emotes)) {
    $body = preg_replace("#{$emote['regex']}#i", "<i class='sprite-emote {$emote['class']}'></i>", $body);
}
/* ...other parsing... */
return $body;
Related Topic