Jump to content

[SOLVED] BB Code with Smilies simple question


gtal3x

Recommended Posts

Hello yestarday someone gave me a code that converts bbcode to html wich works perfectly, all i wont now is to add to this code an array variable for smilies. I have got the smilies array, but i dont know how to add this variable to the first code...

Thats the bbcode to html allone:

function bb2html($text)
{
  $bbcode = array("<", ">",
                "[list]", "[*]", "[/list]", 
                "[img=", "]", 
                "[b]", "[/b]", 
                "[u]", "[/u]", 
                "[i]", "[/i]",
                '[color="', "[/color]",
                "[size=\"", "[/size]",
                '[url]', "[/url]",
                "[mail=\"", "[/mail]",
                "[quote]", "[/quote]",
                '\\"]');
  $htmlcode = array("<", ">",
                "<ul>", "<li>", "</ul>", 
                "<img src=\"", "\">", 
                "<b>", "</b>", 
                "<u>", "</u>", 
                "<i>", "</i>",
                "<span style=\"color:", "</span>",
                "<span style=\"font-size:", "</span>",
                '<a href="', "</a>",
                "<a href=\"mailto:", "</a>",
                "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
                '">');
  $newtext = str_replace($bbcode, $htmlcode, $text);
  //$newtext = nl2br($newtext);//second pass
  return $newtext;
}

And here is the array variable for smilies:

$smilies = array(
  "" => "smile1.gif",
  "" => "wink.gif",
  "" => "grin.gif",
  "" => "tongue.gif",
  "" => "sad.gif",
  ":'(" => "cry.gif",
  ":|" => "noexpression.gif",
  ":-/" => "confused.gif",
  ":-O" => "ohmy.gif",
  "" => "cool1.gif",
  "O:-" => "angel.gif",
  "" => "sleep.gif",
  ":grrr:" => "angry.gif",
  ":smile:" => "smile2.gif",
  "" => "laugh.gif",
  ":cool:" => "cool2.gif",
  ":fun:" => "fun.gif",
  ":thumbsup:" => "thumbsup.gif",
  ":thumbsdown:" => "thumbsdown.gif",
  ":blush:" => "blush.gif",
);

Thanks in advance

Link to comment
Share on other sites

Just create another function

ie

<?php

function smillies($text)
{
$smilies = array(
  "" => "smile1.gif",
  "" => "wink.gif",
  "" => "grin.gif",
  "" => "tongue.gif",
  "" => "sad.gif",
  ":'(" => "cry.gif",
  ":|" => "noexpression.gif",
  ":-/" => "confused.gif",
  ":-O" => "ohmy.gif",
  "" => "cool1.gif",
  "O:-" => "angel.gif",
  "" => "sleep.gif",
  ":grrr:" => "angry.gif",
  ":smile:" => "smile2.gif",
  "" => "laugh.gif",
  ":cool:" => "cool2.gif",
  ":fun:" => "fun.gif",
  ":thumbsup:" => "thumbsup.gif",
  ":thumbsdown:" => "thumbsdown.gif",
  ":blush:" => "blush.gif"
);

$smiliesK = array_keys($smilies);
$newtext = str_replace($smiliesK, "<img src='$smilies'>", $text); //may need to add image path
return $newtext;
}
?>

 

**untested

 

use just like the bbcode

Link to comment
Share on other sites

any solution to make this look like that:

function bb2html($text)
{
  $bbcode = array("<", ">",
                "[list]", "[*]", "[/list]", 
                "[img=", "]", 
                "[b]", "[/b]", 
                "[u]", "[/u]", 
                "[i]", "[/i]",
                '[color="', "[/color]",
                "[size=\"", "[/size]",
                '[url]', "[/url]",
                "[mail=\"", "[/mail]",
                "[quote]", "[/quote]",
                '$smilie[]'    // <------------- I know this variable is wrong just dont know how to do it
                '\\"]');
  $htmlcode = array("<", ">",
                "<ul>", "<li>", "</ul>", 
                "<img src=\"", "\">", 
                "<b>", "</b>", 
                "<u>", "</u>", 
                "<i>", "</i>",
                "<span style=\"color:", "</span>",
                "<span style=\"font-size:", "</span>",
                '<a href="', "</a>",
                "<a href=\"mailto:", "</a>",
                "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
                "[img=$smile[]]" // <----------- Into this
                '">');
  $newtext = str_replace($bbcode, $htmlcode, $text);
  //$newtext = nl2br($newtext);//second pass
  return $newtext;
}

Link to comment
Share on other sites

Ok i have this code now but i get an error for some reason... am really not good with arrays so just dont know how to make it work...

$body = "";

$smilies = array(
  "" => "smile1.gif",
  "" => "wink.gif",
  "" => "grin.gif",
  "" => "tongue.gif",
  "" => "sad.gif",
  ":'(" => "cry.gif",
  ":|" => "noexpression.gif",
  ":-/" => "confused.gif",
  ":-O" => "ohmy.gif",
  "" => "cool1.gif",
  "O:-" => "angel.gif",
  "" => "sleep.gif",
  ":grrr:" => "angry.gif",
  ":smile:" => "smile2.gif",
  "" => "laugh.gif",
  ":cool:" => "cool2.gif",
  ":fun:" => "fun.gif",
  ":thumbsup:" => "thumbsup.gif",
  ":thumbsdown:" => "thumbsdown.gif",
  ":blush:" => "blush.gif",
);

function bb2html($text)
{
  $bbcode = array("<", ">",
                "[list]", "[*]", "[/list]", 
                "[img=", "]", 
                "[b]", "[/b]", 
                "[u]", "[/u]", 
                "[i]", "[/i]",
                '[color="', "[/color]",
                "[size=\"", "[/size]",
                '[url]', "[/url]",
                "[mail=\"", "[/mail]",
                "[quote]", "[/quote]",
                '\\"]');
  $htmlcode = array("<", ">",
                "<ul>", "<li>", "</ul>", 
                "<img src=\"", "\">", 
                "<b>", "</b>", 
                "<u>", "</u>", 
                "<i>", "</i>",
                "<span style=\"color:", "</span>",
                "<span style=\"font-size:", "</span>",
                '<a href="', "</a>",
                "<a href=\"mailto:", "</a>",
                "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
                '">');
  $newtext = str_replace($bbcode, $htmlcode, $text);
  //$newtext = nl2br($newtext);//second pass
$smiliesK = array_keys($smilies);
$newtext = str_replace($smiliesK, "<img src='images/smilies/$smilies'>", $text);
  return $newtext;
}

echo bb2html($body);

The error that am getting is:

Warning: array_keys() [function.array-keys]: The first argument should be an array in C:\...........

Link to comment
Share on other sites

if you are using the array in the function then you must put the array in the function...

$body = "";

function bb2html($text)
{
  $bbcode = array("<", ">",
                "[list]", "[*]", "[/list]", 
                "[img=", "]", 
                "[b]", "[/b]", 
                "[u]", "[/u]", 
                "[i]", "[/i]",
                '[color="', "[/color]",
                "[size=\"", "[/size]",
                '[url]', "[/url]",
                "[mail=\"", "[/mail]",
                "[quote]", "[/quote]",
                '\\"]');
  $htmlcode = array("<", ">",
                "<ul>", "<li>", "</ul>", 
                "<img src=\"", "\">", 
                "<b>", "</b>", 
                "<u>", "</u>", 
                "<i>", "</i>",
                "<span style=\"color:", "</span>",
                "<span style=\"font-size:", "</span>",
                '<a href="', "</a>",
                "<a href=\"mailto:", "</a>",
                "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
                '">');
  $newtext = str_replace($bbcode, $htmlcode, $text);
  //$newtext = nl2br($newtext);//second pass

$smilies = array(
  "" => "smile1.gif",
  "" => "wink.gif",
  "" => "grin.gif",
  "" => "tongue.gif",
  "" => "sad.gif",
  ":'(" => "cry.gif",
  ":|" => "noexpression.gif",
  ":-/" => "confused.gif",
  ":-O" => "ohmy.gif",
  "" => "cool1.gif",
  "O:-" => "angel.gif",
  "" => "sleep.gif",
  ":grrr:" => "angry.gif",
  ":smile:" => "smile2.gif",
  "" => "laugh.gif",
  ":cool:" => "cool2.gif",
  ":fun:" => "fun.gif",
  ":thumbsup:" => "thumbsup.gif",
  ":thumbsdown:" => "thumbsdown.gif",
  ":blush:" => "blush.gif",
);

but personally i would use my first suggestion!
$smiliesK = array_keys($smilies);
$newtext = str_replace($smiliesK, "<img src='images/smilies/$smilies'>", $text);
  return $newtext;
}

echo bb2html($body);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.