preg_replace()找不到结束定界符? [英] preg_replace() not finding ending delimiter?
问题描述
我经常使用preg_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;
}
?>
但是今天我试图对其进行修改,并使用mysql让我随心所欲地插入和删除它们,而不用玩我的代码,但是当我尝试它时,它只会抛出
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
警告:preg_replace()[function.preg-replace]:没有结束定界符 在226行的PATH/TO/FILE.php中找到#"
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);
}
}
?>
那是行不通的,是的,正则表达式行包含定界符,因此它将输出#\:p#
That didn't work, and yes the regex row included the delimiters so it would output #\:p#
我遇到了与前面所述相同的错误,然后我尝试从MySQL的数据中删除#
,然后按如下所示更改了preg_replace
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.
推荐答案
由于人们仍然不赞成该主题. @salathe在问题注解中是正确的(循环中返回.哎呀).
Since people are still downvoting this topic. @salathe was correct in the questions comments (returning in the loop.. Ooops).
但这是答案:
$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()找不到结束定界符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!