【发布时间】:2012-05-03 19:13:56
【问题描述】:
我有这个功能:
function bb_parse($string) {
$string = $this->quote($string);
$string=nl2br($string);
$string = html_entity_decode(stripslashes(stripslashes($string)));
$tags = 'b|i|u';
while (preg_match_all('`\[('.$tags.')=?(.*?)\](.+?)\[/\1\]`', $string, $matches)) foreach ($matches[0] as $key => $match) {
list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);
switch ($tag) {
case 'b': $replacement = "<strong>$innertext</strong>"; break;
case 'i': $replacement = "<em>$innertext</em>"; break;
case 'u': $replacement = "<u>$innertext</u>"; break;
}
$string = str_replace($match, $replacement, $string);
}
return $string;
}
如您所见,我可以轻松地制作带有粗体、斜体和下划线的 BBCode。虽然,我也在尝试为这个功能添加笑脸,但没有运气。
我尝试简单地将 :) 添加到 $tags,然后在案例中添加笑脸 :) img,但这不起作用。
我该怎么做,这样我也可以添加表情符号?
提前致谢。
【问题讨论】:
-
您可能希望通过单独的
str_replace调用来制作表情符号,因为它们不需要正则表达式解析配对标签。 -
:)不是标签。这就是你的正则表达式失败的原因。 -
类似问题,其答案包括您可能需要考虑的其他一些问题:Match and replace emoticons in string - what is the most efficient way?。 (tchrist 的回答很有趣。)