Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> LiveThunder's BBcodes and smilies


 
satish
post Jan 11 2008, 02:21 AM
Post #1


Admin
Group Icon
Group: Root Admin
Posts: 980
Joined: 8-October 06
From: In my server...
Member No.: 1





Introduction

In the last time I have seen a couple of questions on the forums about how to parse smilies, so I decided to write a tutorial on it, but since BBcodes are related to smilies I decided to put that in my tutorial as well.

In this tutorial we will create a class that will take care of the conversion from BBcode to XHTML 1.1 compliant code and it will be able to parse smilies into images.

Please note that this tutorial will expect some basic knowledge about classes and OOP. If you are unfamiliar with that subject you may see my tutorial called Introduction to Object Oriented Programming in PHP4. A bit of regular expression knowledge would be useful as well.

The parser will have the following features

* Parse smileys
* Parse BBcodes:
o Links
o Email links
o Bold
o Italic
o Underline
o Center (alignment)
o Right (alignment)
o Images
* XHTML 1.1 compliant code
The initial class

We will start by creating our class, a constructor for it, make some options and create the functions we will need:
CODE
<?php
class parser
{
    var $parse_bbcodes; // Boolean - true = on | false = off
    var $parse_smilies; // Boolean - true = on | false = off
    var $smiley_url;    // String  - sets the URL to the smiley folder
    
    function parser()
    {
        // Set some default values:
        $this->parse_bbcodes = true;
        $this->parse_smilies = true;
        $this->smiley_url    = "/smileys";
    }
    
    function parse($data)
    {
        
    }
    
    function _parse_bbcodes($data)
    {
        
    }
    
    function _parse_smilies($data)
    {
        
    }
}
?>



The first function (parser) is the constructor. It is run when an object of the class parser is created. The second function will be the function that the user will call, with the text to parse in the parameter called $data. The next too functions is called by the function parse and will parse bbcodes and smilies, respectively.
Creating the parse function

The parse function is very simple:

CODE
function parse($data)
{
    $data = htmlentities($data);
    
    if($this->parse_bbcodes)
    {
        $data = $this->_parse_bbcodes($data);
    }
    if($this->parse_smilies)
    {
        $data = $this->_parse_smilies($data);
    }
        
    $data = nl2br($data);
    
    return $data;
}



The nl2br function converts linebreaks to HTML linebreaks. The htmlentities function makes sure that any HTML code the user input will just be shown. The rest should be fairly simple.

Parsing smilies

First I will start by showing you the entire parse smilies function, the I will tell about it:

CODE
function _parse_smilies($data)
{
    $smilies = array(
            ':)' => 'smile.gif',
            ':(' => 'sad.gif',
        );
    
    if(substr($this->smiley_url,-1) != '/' && !empty($this->smiley_url))
    {
        $this->smiley_url .= '/';
    }
    
    foreach($smilies as $code => $image)
    {
        $data = str_replace($code,"<img src='{$this->smiley_url}{$image}' alt='{$code}' />",$data);
    }

    return $data;
}



First we have an array of smilies where the key is the code, and the value is the filename. Next we will check if the smiley url has a trailing slash, else we will add it. Then we run through each smiley and replace the code with an image. Finally we return the data again.
Parsing the BBcodes

Parsing the BBcodes is probably the most complex thing in this tutorial (not too complex though). First the entire function:

CODE
function _parse_bbcodes($data)
{
    $data = preg_replace("`[(b|i|u|url|mail|img|center|right)][/(b|i|u|url|mail|img|center|right)]`",'',$data);
    
    $data = preg_replace("`[url](.*)[/url]`sUi","<a href='\1'>\1</a>",$data);
    $data = preg_replace("`[url=(.*)](.*)[/url]`sUi","<a href='\1'>\2</a>",$data);
    
    $data = preg_replace("`[mail](.*)[/mail]`sUi","<a href='mailto:\1'>\1</a>",$data);
    $data = preg_replace("`[mail=(.*)](.*)[/mail]`sUi","<a href='mailto:\1'>\2</a>",$data);
    
    $data = preg_replace("`[b](.*)[/b]`sUi","<span style='font-weight: bold;'>\1</span>",$data);
    $data = preg_replace("`[i](.*)[/i]`sUi","<span style='font-style: italic;'>\1</span>",$data);
    $data = preg_replace("`[u](.*)[/u]`sUi","<span style='text-decoration: underline;'>\1</span>",$data);
    
    $data = preg_replace("`[center](.*)[/center]`sUi","<div style='text-align: center;'>\1</div>",$data);
    $data = preg_replace("`[right](.*)[/right]`sUi","<div style='text-align: right;'>\1</div>",$data);
    
    $data = preg_replace("`[img](.*)[/img]`sUi","<img src='\1' alt='Posted Image' style='border: 0px;' />",$data);
    
    return $data;
}



The first function removes BBcode tags with nothing in them, just to make the output smaller. The next functions deals with: URLs, email links, bold, italic, underline, center (alignment), right (alignment) and images.

You could extend this with colors, sizes, other fonts, ordered lists and unorders lists and lots of other things.

For more information about regular expressions, consult the PHP manual on Regular Expression Functions
Full code

For your convenience I will show you the entire code on this page:


CODE
<?php
class parser
{
    var $parse_bbcodes; // Boolean - true = on | false = off
    var $parse_smilies; // Boolean - true = on | false = off
    var $smiley_url;    // String  - sets the URL to the smiley folder
    
    function parser()
    {
        // Set some default values:
        $this->parse_bbcodes = true;
        $this->parse_smilies = true;
        $this->smiley_url    = "/smileys";
    }
    
    function parse($data)
    {
        $data = htmlentities($data);
        
        if($this->parse_bbcodes)
        {
            $data = $this->_parse_bbcodes($data);
        }
        if($this->parse_smilies)
        {
            $data = $this->_parse_smilies($data);
        }
        
        $data = nl2br($data);
        
        return $data;
    }
    
    function _parse_bbcodes($data)
    {
        $data = preg_replace("`[(b|i|u|url|mail|img|center|right)][/(b|i|u|url|mail|img|center|right)]`",'',$data);
        
        $data = preg_replace("`[url](.*)[/url]`sUi","<a href='\1'>\1</a>",$data);
        $data = preg_replace("`[url=(.*)](.*)[/url]`sUi","<a href='\1'>\2</a>",$data);
        
        $data = preg_replace("`[mail](.*)[/mail]`sUi","<a href='mailto:\1'>\1</a>",$data);
        $data = preg_replace("`[mail=(.*)](.*)[/mail]`sUi","<a href='mailto:\1'>\2</a>",$data);
        
        $data = preg_replace("`[b](.*)[/b]`sUi","<span style='font-weight: bold;'>\1</span>",$data);
        $data = preg_replace("`[i](.*)[/i]`sUi","<span style='font-style: italic;'>\1</span>",$data);
        $data = preg_replace("`[u](.*)[/u]`sUi","<span style='text-decoration: underline;'>\1</span>",$data);
        
        $data = preg_replace("`[center](.*)[/center]`sUi","<div style='text-align: center;'>\1</div>",$data);
        $data = preg_replace("`[right](.*)[/right]`sUi","<div style='text-align: right;'>\1</div>",$data);
        
        $data = preg_replace("`[img](.*)[/img]`sUi","<img src='\1' alt='Posted Image' style='border: 0px;' />",$data);
        
        return $data;
    }
    
    function _parse_smilies($data)
    {
        $smilies = array(
                ':)' => 'smile.gif',
                ':(' => 'sad.gif',
            );
        
        if(substr($this->smiley_url,-1) != '/' && !empty($this->smiley_url))
        {
            $this->smiley_url .= '/';
        }
        
        foreach($smilies as $code => $image)
        {
            $data = str_replace($code,"<img src='{$this->smiley_url}{$image}' alt='{$code}' />",$data);
        }
        
        return $data;
    }
}
?>



Testing the code:

You may use this to test the parser. Remember to change parser.php to something else if you named your parser file something else. Note that you will have to actually create some smiley images, add them to your array and define the smiley url before you can use the parser to parse smileys. Else you may just view the HTML source to see that it has actually parsed it.



CODE
<?php
include 'parser.php';
$parser = new parser();

$default_text = <<<EOF
This is a sample text. [url=http://phpfreaks.com]PHP Freaks[/url] is a PHP website :)

[b]bold[/b] [b]bold again[/b] [i]italic[/i] [u]underline[/u]

left
[center]center[/center]
[right]right[/right]

[url]http://phpfreaks.com[/url]

[mail=somebody@example.com]Somebody's mail[/mail]
[mail]somebody@example.com[/mail]

[center][url=http://google.com][img]http://www.google.com/intl/en/images/logo.gif[/img][/url][/center]
EOF;

$text = !empty($_POST['text']) ? $_POST['text'] : $default_text;
$ps_checked = (bool)$_POST['parse_smilies'] ? " checked='checked'" : null;
$pbbc_checked = (bool)$_POST['parse_bbcodes'] ? " checked='checked'" : null;

echo <<<EOF
<form action='{$_SERVER['REQUEST_URI']}' method='post'>
<label><input type='checkbox' name='parse_smilies' value='1'{$ps_checked}> Parse smilies</input></label><br />
<label><input type='checkbox' name='parse_bbcodes' value='1'{$pbbc_checked}> Parse BBcodes</input></label><br /><br />

<label>Text to parse:<br /><textarea cols='10' rows='15' style='width: 100%;' name='text'>{$text}</textarea></label><br /><br />

<button type='submit'>Parse text</button>
</form>
EOF;

if(!empty($_POST['text']))
{
    $parser->parse_smilies = (bool) $_POST['parse_smilies'];
    $parser->parse_bbcodes = (bool) $_POST['parse_bbcodes'];
    $text = $parser->parse($_POST['text']);
    
    echo <<<EOF

<hr />

<div style='overflow: auto; height: 200px;'>{$text}</div>

EOF;
}
?>


Conclusion

This is the end of my smilies and BBcodes tutorial. You should have learned something about regular expressions, perhaps a little bit about OOP and of course how you make a smiley and BBcode converter.

Happy Programming.


--------------------
Go to the top of the page
 
+Quote Post

 
Ken
post Feb 6 2008, 04:33 AM
Post #2


Newbie
Group Icon
Group: Members
Posts: 3
Joined: 6-February 08
Member No.: 6,487





Hello,

I tried this tutorial before I could implement it, and this code did not work? mellow.gif

I am not getting any error message, but the smilies I used (smile and sad) is not showing at all.

I named the file bbcsmilies.php and put it in include folder. Then I added this code on top of header.php

CODE
include ("./include/bccsmilies.php");


I editted the link ../smilies/ (yea, I named my smilies folder smilies) and it did not come up so I tried a full link http://www.sitename.com/smilies and then added / at the end. Still nothing and no error message?

Thanks!
Go to the top of the page
 
+Quote Post

 
GAMA® The Ultima...
post Feb 6 2008, 10:49 PM
Post #3


Member
Group Icon
Group: Members
Posts: 23
Joined: 24-April 07
Member No.: 528





Check if ur php is enabled to show you errors.


--------------------


Go to the top of the page
 
+Quote Post

 
Ken
post Feb 7 2008, 12:55 AM
Post #4


Newbie
Group Icon
Group: Members
Posts: 3
Joined: 6-February 08
Member No.: 6,487





QUOTE (GAMA® The Ultimate Gamer.. @ Feb 6 2008, 05:49 PM) *
Check if ur php is enabled to show you errors.


What do you mean by that? My php on my server is working fine and I have been working on few codes for 3 weeks now running into thousand of errors and debugged them. It is just odd I am not getting any error from your code, that's all. I just thought you would have some idea where to point me why I am not getting any error nor result.
Go to the top of the page
 
+Quote Post

 
satish
post Feb 8 2008, 12:42 AM
Post #5


Admin
Group Icon
Group: Root Admin
Posts: 980
Joined: 8-October 06
From: In my server...
Member No.: 1





Like for example. sometimes your php is configured to hide errors. if u have access to php files u can do it yourself or ask your host.


--------------------
Go to the top of the page
 
+Quote Post

 
Prezadent
post Apr 26 2008, 07:51 AM
Post #6


Newbie
Group Icon
Group: Members
Posts: 1
Joined: 26-April 08
Member No.: 6,792





QUOTE (satish @ Feb 8 2008, 12:42 AM) *
Like for example. sometimes your php is configured to hide errors. if u have access to php files u can do it yourself or ask your host.


Doesn't work, no error message.
Go to the top of the page
 
+Quote Post

 
KaLLe AnkA
post Apr 26 2008, 10:35 AM
Post #7


Junior Member
Group Icon
Group: Members
Posts: 88
Joined: 29-January 08
Member No.: 6,443





Thank you alot, i have always want this


--------------------
Go to the top of the page
 
+Quote Post

 
Ken
post Apr 26 2008, 12:02 PM
Post #8


Newbie
Group Icon
Group: Members
Posts: 3
Joined: 6-February 08
Member No.: 6,487





QUOTE (Prezadent @ Apr 26 2008, 02:51 AM) *
Doesn't work, no error message.


I am with prezadent.. no error output.
Go to the top of the page
 
+Quote Post

 
FHN
post Jul 22 2008, 09:43 AM
Post #9


Newbie
Group Icon
Group: Members
Posts: 7
Joined: 22-July 08
Member No.: 7,238





I really appreciate this. I was Looking for something like this.


--------------------
We don't need the key We'll break in
Go to the top of the page
 
+Quote Post

 
DeadlySins
post Aug 29 2008, 12:33 PM
Post #10


Admin
Group Icon
Group: Admin
Posts: 105
Joined: 1-August 08
From: Merced CA
Member No.: 7,282





thank you


--------------------
Go to the top of the page
 
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: