Jump to content

Parsing Help (BBCode) - New Topic


LemonInflux

Recommended Posts

parse 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=(.*)]`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;
    }
}
?>

 

test code:

 

<?php
include 'bbcode.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]email[/mail]
[mail]somebody@example.com[/mail]


[center][url=http://google.com][img=http://www.google.com/intl/en/images/logo.gif][/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;
}
?>

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.