tags:

views:

57

answers:

5

Hello I need some help.

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>',
    '<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 &quot; and so on

+1  A: 

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);
Jonathan Kuhn
He still needs to do the other replacements though, I think.
Faisal
but i still want the other bbcodes
Basta
oh, then pass in a second parameter to the bbcode function that is $hide_smilies, give it a default value of false (or 0). In the function, make two arrays of replacements, one for smilies and one for other. Then do the preg_replace for both arrays. If I had more time I would fix it for you.
Jonathan Kuhn
A: 

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);
}
?>
Mark Eirich
A: 

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);
}
Faisal
hm it works but now " get replaced with " etc
Basta
That probably has to do with you calling htmlentities() at the beginning of your function. If you don't want special characters to be replaced by their html entity equivalents you can comment that out, but watch out for weird characters not getting encoded properly.
Faisal
but it didnt get like this before i have always had htmlentities in top. it only got replaced if you look at webpage source. but now all my posts look crazy
Basta
Well, I corrected some mistakes in your syntax, like not including a closing ")" for your call to array() for $replace. (Compare the two functions side by side and you'll see it.) Was that just a typo when you retyped it, or was that in the original code>?
Faisal
A: 
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);
}
stormdrain
A: 

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/";
}
George Marian
hm it works but now my text characters like " get replaced with " etc
Basta
@Basta Take my code and work it into your function. Replace the section where you create $find in your code with what I have and add my if statement below that. And, of course, replace the function line with what I have. Sorry, I should've mentioned that it isn't the complete code in my post.
George Marian