Php preg_replace()找不到结束分隔符?
我经常使用preg_替换,但我不是这方面的天才 如果我启动一个函数并故意输入我想要使用的所有微笑,例如Php preg_replace()找不到结束分隔符?,php,mysql,preg-replace,preg-match,Php,Mysql,Preg Replace,Preg Match,我经常使用preg_替换,但我不是这方面的天才 如果我启动一个函数并故意输入我想要使用的所有微笑,例如 <?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
<?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;
}
?>
它还是不喜欢它?我还尝试分配:
$regex = $row['regex'];
preg_replace("#$regex#i");
你猜怎么着?仍然是否定的。非常感谢您提供有关如何使用此功能的任何帮助。从
preg_replace('#' . $row['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;
这是因为可以使用开始和结束正则表达式。你需要做一些像/$row['regex']./对不起,我应该说明的是开始和结束我的表达,然后你需要逃避它,如果你也要使用磅符号。\$行['regex'],可以使用| |或{}。另外,我假设$row['regex']只是。你真的需要使用preg_replace吗?你能使用str_replace吗?你知道,由于循环中的返回,你的代码只会尝试替换从数据库返回的第一个emote吗?@devxdev请不要给出不完整的示例。
preg_replace('#' . $row['regex'] . '#i', .......)
preg_replace('/' . $row['regex'] . '/i', .......)
$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;