Look, if forum post have $hide_smilies is set to 1, I dont want the :p, :o to be replacen with images.
This is how I output forum post bbcode($message);
And Function:
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>',
'&开发者_高级运维lt;i>$1</i>',
$str = preg_replace($find, $replace, $str);
return nl2br($str);
Thanks
Edit
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);
}
This works but now (if hide_smilies=0) some characters like " gets replaced with "
and so on
if hide smilies is set to 1 then just echo out $message instead of echo'ing out bbcode($message). here is a simple ternary statement that should work:
echo ($hide_smilies==1) ? $message : bbcode($message);
Just use array_slice() to chop off the unwanted bits. I'm assuming you can pass the $hide_smilies variable to the bbcode() function.
<?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);
}
?>
If I'm understanding correctly, you still want to replace [b]'s and [i]'s with their HTML equivalents even if $hide_smilies is 1, right? In that case, initialize each array with only the non-smiley pattenrs and then add the extra elements if $hide_smilies = 1. For example:
// 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);
}
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);
}
Just add a parameter to the function and change the way you build the $find array, accordingly.
function bbcode($str, $hideSmilies = false)
{
$find = array(
'/\[b](.*?)\[\/b]/is',
'/\[u](.*?)\[\/u]/is',
'/\[i](.*?)\[\/i]/is'
);
if (!$hideSmilies)
{
$find[] = "/:p/";
$find[] = "/:o/";
}
精彩评论