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);
}
}
?>
那不起作用,是的,正则表达式行包含分隔符,因此它将输出 #\:p #
我得到了与之前所述相同的错误,然后我尝试从 MySQL 中的数据中取出 #
并按如下方式更改 preg_replace
preg_replace('#' . $row['regex'] . '#i', .......)
,它还是不喜欢?我也尝试过分配:
$regex = $row['regex'];
preg_replace("#$regex#i");
你猜怎么着?还是不行。非常感谢任何关于如何处理此问题的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为人们仍然对这个话题投反对票。 @salathe 在问题评论中是正确的(在循环中返回..哎呀)。
但答案是这样的:
Since people are still downvoting this topic. @salathe was correct in the questions comments (returning in the loop.. Ooops).
but here is the answer:
将您的代码从 更改
为
Change your code from
to