gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:35:53 26 4
gpt4 key购买 nike

我经常使用 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 让我可以随意插入和删除它们而无需在我的代码中播放,但是当我尝试它时,它只会抛出

Warning: preg_replace() [function.preg-replace]: No ending delimiter '#' found in PATH/TO/FILE.php on line 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");

你猜怎么着?还是不行。非常感谢任何有关如何进行此操作的帮助。

最佳答案

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

关于php - preg_replace() 找不到结束分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7619476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com