Author Topic: [SOLVED] BBcode - adding smilies  (Read 304 times)

0 Members and 1 Guest are viewing this topic.

Offline MasterACE14Topic starter

  • Addict
  • Posts: 2,646
  • Gender: Male
  • Programming, the art of combining math and logic.
    • View Profile
    • Crikey Games Pty Ltd
[SOLVED] BBcode - adding smilies
« on: November 21, 2008, 03:38:47 AM »
I have this BBcode:
Code: [Select]
<?php
// bbcode
function bbcode($string)
{
    
// All the default bbcode arrays.
    
$bbcode = array(
        
//Text Apperence
        
'#\[b\](.*?)\[/b\]#si' => '<b>\\1</b>',
        
'#\[i\](.*?)\[/i\]#si' => '<i>\\1</i>',
        
'#\[u\](.*?)\[/u\]#si' => '<u>\\1</u>',
       
// '#\[s\](.*?)\[/s\]#si' => '<strike>\\1</strike>',
        //Font Color
        
'#\[color=(.*?)\](.*?)\[/color\]#si' => '<font color="\\1">\\2</font>',
        
//Text Effects
       // '#\[bl\](.*?)\[/bl\]#si' => '<blink>\\1</blink>',
       // '#\[marquee\](.*?)\[/marquee\]#si' => '<marquee>\\1</marquee>',
        //Other
       // '#\[code\](.*?)\[/ code]#si' => '<div class="bbcode_code_title">CODE:</div><div class="bbcode_code_code">\\1<div>',
        
'#\[url=(.*?)\](.*?)\[/url]#si' => '<a href="\\1" target="_blank">\\2</a>',
       
// '#\[quote\](.*?)\[/quote\]#si' => '<div class="bbcode_quote_title">CODE:</div><div class="bbcode_quote_quote">\\1<div>',
       // '#\[img\](.*?)\[/img\]#si' => '<img src="\\1">',
        
'#\[email\](.*?)\[/email\]#si' => '<a href="mailto:\\1">\\1</a>'
    
);
    
$output preg_replace(array_keys($bbcode), array_values($bbcode), $string);
   return 
nl2br($output);
}

and I want to add smilies.

like...
Quote
: )
would be replaced with

I've tried a few things with no luck.

Any help is greatly appreciated.

Regards ACE
« Last Edit: November 21, 2008, 03:39:36 AM by MasterACE14 »
Why is hashing a hash bad practice?
Quote from: Zane
A business person’s best customers are always using the cheapest piece of shit computer out there. I don’t have the sources to back it up, but I’d like to say it’s a proven fact.
Quote from: requinix
Use objects when they make sense and functions when they don't.
Crikey Games Pty Ltd | Realm Battles Classic
It's too big a world to be in competition with everyone.  The only person who I have to be better than is myself. ~Harry Morgan

Offline Jaysonic

  • Fanatic
  • Posts: 3,332
  • Gender: Male
  • 8548863
    • View Profile
    • I Am Lewy Babes
Re: BBcode - adding smilies
« Reply #1 on: November 21, 2008, 03:48:41 AM »
str_replace() is fine for this.

Code: [Select]
<?php

$what 
= array(
":)",
":D",
":P"
);
$with = array(
"<img src='smile.gif' alt='Smile' />",
"<img src='grin.gif' alt='Grin' />",
"<img src='tongue.gif' alt='Tongue' />"
);
$content str_replace($what$with$content);
?>


Or you could set it up how you have, using array_keys() and array_values().
« Last Edit: November 21, 2008, 03:49:27 AM by ProjectFear »
Good luck with your coding.
Jason ~ ProjectFear ~ Jaysonic (and variations of... :))

Offline MasterACE14Topic starter

  • Addict
  • Posts: 2,646
  • Gender: Male
  • Programming, the art of combining math and logic.
    • View Profile
    • Crikey Games Pty Ltd
Re: BBcode - adding smilies
« Reply #2 on: November 21, 2008, 04:42:59 AM »
that worked perfectly. Cheers.

Why is hashing a hash bad practice?
Quote from: Zane
A business person’s best customers are always using the cheapest piece of shit computer out there. I don’t have the sources to back it up, but I’d like to say it’s a proven fact.
Quote from: requinix
Use objects when they make sense and functions when they don't.
Crikey Games Pty Ltd | Realm Battles Classic
It's too big a world to be in competition with everyone.  The only person who I have to be better than is myself. ~Harry Morgan