0

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

3
  • "This works but now (if hide_smilies=0) some characters like " gets replaced with &quot; and so on" Are you sure this only happens when you hide smilies? You're calling htmlentities() at the very top of the function, that's what it does: php.net/manual/en/function.htmlentities.php Commented Jul 14, 2010 at 3:48
  • well i did the first time i reloaded, maybe it didnt reload good. but how can you make your output safe then?? if you cant use htmlentities Commented Jul 14, 2010 at 4:05
  • I wasn't suggesting that you should not use htmlentities(). Based on that code, there is no reason that it should happen only if hide_smilies=0. It should happen in either case. That problem may be double encoding. That is, it may be that the strings are passed through htmlentities() [or something that has similar functionality] more than once. I would also make sure that there are not issue w/ the HTML source itself. Commented Jul 14, 2010 at 5:06

5 Answers 5

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

He still needs to do the other replacements though, I think.
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.
0

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);
}
?>

Comments

0

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);
}

3 Comments

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.
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
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>?
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);
}

Comments

0

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/";
}

2 Comments

hm it works but now my text characters like " get replaced with &quot; etc
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.