preg_replace() 找不到结束分隔符?

发布于 2024-12-07 10:05:58 字数 1218 浏览 1 评论 0原文

我经常使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-12-14 10:05:58

因为人们仍然对这个话题投反对票。 @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;

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;
梦言归人 2024-12-14 10:05:58

将您的代码从 更改

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

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

Change your code from

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

to

preg_replace('/' . $row['regex'] . '/i', .......)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文