Preg_replace()找不到结束分隔符



我经常使用preg_replace(),但我不是一个天才。

如果我启动一个函数并故意键入我想要使用的所有符号,例如

<?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;
}
?>

,但今天我试图改变它,并使用mysql让我只是插入和删除他们,因为我不玩我的代码,但当我尝试它,它只抛出

警告:preg_replace() [function。[preg-replace]:没有结束分隔符在PATH/TO/FILE.php第226行发现'#'

下面是我第一次使用的代码:

<?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);
    }
}
?>

这不起作用,是的,regex行包含分隔符,因此它将输出#:p#

我得到了与之前相同的错误,然后我试图将# s从MySQL中的数据中取出,并更改了preg_replace,如下所示

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

它仍然不喜欢它?我也试过分配:

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

你猜怎么着?还是不行。如果有任何帮助,我将不胜感激。

既然人们仍然在对这个话题投否定票。@salathe在问题评论中是正确的(在循环中返回)。糟糕)。

但这里是答案:

$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;

更改代码
preg_replace('#' . $row['regex'] . '#i', .......)

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

最新更新