看,如果论坛帖子将$ hide_smilies设置为1,我不希望:p,:o替换为图像。
这是我输出论坛帖bbcode($message);
功能:
function bbcode($str)
{
$str = htmlentities($str);
$find = array(
"/:p/",
"/:o/",
'/\[b](.*?)\[\/b]/is',
'/\[u](.*?)\[\/u]/is',
'/\[i](.*?)\[\/i]/is'
);
$replace = array(
'<img src="/images/forum/icon_tongue.gif" alt=":p" border="0" height="15" width="15">',
'<img src="/images/forum/icon_embarrassed.gif" alt=":o" border="0" height="15" width="15">'
'<strong>$1</strong>',
'<u>$1</u>',
'<i>$1</i>',
$str = preg_replace($find, $replace, $str);
return nl2br($str);
由于
function bbcode($str, $hide_smilies = 0)
{
$str = htmlentities($str);
$find = array(
'/\[b](.*?)\[\/b]/is',
'/\[u](.*?)\[\/u]/is',
'/\[i](.*?)\[\/i]/is',
);
$replace = array(
'<strong>$1</strong>',
'<u>$1</u>',
'<i>$1</i>'
);
if ($hide_smilies == 0)
{
$find[] = "/:p/";
$find[] = "/:o/";
$replace[] = '<img src="/images/forum/icon_tongue.gif" alt=":p" border="0" height="15" width="15">';
$replace[] = '<img src="/images/forum/icon_embarrassed.gif" alt=":o" border="0" height="15" width="15">';
}
$str = preg_replace($find, $replace, $str);
return nl2br($str);
}
这样可行,但现在(如果hide_smilies = 0)某些字符如“被"
替换”等等
答案 0 :(得分:1)
如果隐藏表情符号设置为1,则只回显$ message而不是回显出bbcode($ message)。这是一个应该有效的简单三元语句:
echo ($hide_smilies==1) ? $message : bbcode($message);
答案 1 :(得分:0)
只需使用array_slice()来切断不需要的位。我假设您可以将$ hide_smilies变量传递给bbcode()函数。
<?php
function bbcode($str, $hide_smilies=0) {
$str = htmlentities($str);
$find = array(
"/:p/",
"/:o/",
'/\[b](.*?)\[\/b]/is',
'/\[u](.*?)\[\/u]/is',
'/\[i](.*?)\[\/i]/is',
);
$replace = array(
'<img src="/images/forum/icon_tongue.gif" alt=":p" border="0" height="15" width="15">',
'<img src="/images/forum/icon_embarrassed.gif" alt=":o" border="0" height="15" width="15">',
'<strong>$1</strong>',
'<u>$1</u>',
'<i>$1</i>',
);
if ($hide_smilies) {
$find = array_slice($find, 2);
$replace = array_slice($replace, 2);
}
$str = preg_replace($find, $replace, $str);
return nl2br($str);
}
?>
答案 2 :(得分:0)
如果我理解正确,即使$ hide_smilies为1,你仍然想用它们的HTML等价替换[b]和[i],对吗?在这种情况下,仅使用非笑脸pattenrs初始化每个数组,然后在$ hide_smilies = 1时添加额外元素。例如:
// either pass in $hide_smilies, declare it global inside bbcode(),
// or use $_GLOBALS['hide_smilies']
function bbcode($str, $hide_smilies)
{
$str = htmlentities($str);
$find = array(
'/\[b](.*?)\[\/b]/is',
'/\[u](.*?)\[\/u]/is',
'/\[i](.*?)\[\/i]/is'
);
$replace = array(
'<strong>$1</strong>',
'<u>$1</u>',
'<i>$1</i>');
if ($hide_smilies == 1)
{
$find[] = "/:p/";
$find[] = "/:o/";
$replace[] = '<img src="/images/forum/icon_tongue.gif" alt=":p" border="0" height="15" width="15">';
$replace[] = '<img src="/images/forum/icon_embarrassed.gif" alt=":o" border="0" height="15" width="15">';
}
$str = preg_replace($find, $replace, $str);
return nl2br($str);
}
答案 3 :(得分:0)
function bbcode($str)
{
$str = htmlentities($str);
$find = array(
'/\[b](.*?)\[\/b]/is',
'/\[u](.*?)\[\/u]/is',
'/\[i](.*?)\[\/i]/is'
);
$replace = array(
'<strong>$1</strong>',
'<u>$1</u>',
'<i>$1</i>',
);
$str = preg_replace($find, $replace, $str);
return nl2br($str);
}
答案 4 :(得分:0)
只需在函数中添加一个参数,并相应地更改构建$ find数组的方式。
function bbcode($str, $hideSmilies = false)
{
$find = array(
'/\[b](.*?)\[\/b]/is',
'/\[u](.*?)\[\/u]/is',
'/\[i](.*?)\[\/i]/is'
);
if (!$hideSmilies)
{
$find[] = "/:p/";
$find[] = "/:o/";
}