Publishing System Settings Logout Login Register
Pear Module: HTML_BBCodeParser
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on July 23rd, 2005
7012 views
PHP Coding
HTML_BBCodeParser
This tutorial is about Pear's HTML_BBCodeParser and how to use it.

Table of Content:
1. Introduction to PEAR
2. Introduction to HTML_BBCodeParser
3. Installing HTML_BBCodeParser
4. Using HTML_BBCodeParser

1. Introduction to PEAR
What is PEAR? (from the PEAR website)


PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide:

- A structured library of open-sourced code for PHP users
- A system for code distribution and package maintenance
- A standard style for code written in PHP
- The PHP Foundation Classes (PFC)
- The PHP Extension Community Library (PECL)
- A web site, mailing lists and download mirrors to support the PHP/PEAR community

PEAR is a community-driven project with the PEAR Group as the governing body. The project has been founded by Stig S. Bakken in 1999 and quite a lot of people have joined the project since then.


How do I install PEAR?
The installation guide on how to install PEAR

2. Introduction to HTML_BBCodeParser
This PEAR package transforms plain bbcode text into HTML text, which is very usefull for commenting or forum systems.
HTML_BBCodeParser is the alternative for hours of coding your own, crappy parser.


3. Installing HTML_BBCodeParser
HTML_BBCodeParser can be installed in many different ways. The easiest way is the PEAR package manager. (Root access is required for this)
(requires the latest version of the PEAR package manager to be installed.)

Automatic Installation:
Go to your shell and run:
$ pear install HTML_BBCodeParser

PEAR package manager now downloads and installs the specified package.

Semi-Automatic Installation:
Another way to install a package offline is putting the package in your PEAR dir.
Then run the following command:
$ pear install HTML_BBCodeParser.tgz

This installs the package (downloaded as a .tgz) without needing to have an internet connection.

Manual Installation:
To install the package manually, let's put the package in /includes/
Use the following code to tell PHP where to look for our package:
<?php
ini_set(\"include_path\", &#039;/var/www/www.example.com/includes/&#039;);
?>


Now we have set our include_path, we can simply require our package like this:
require_once 'HTML/BBCodeParser.php';


After installing HTML_BBCode Parser, read on for the instructions on how to use it.

4. Using HTML_BBCodeParser
This step of the tutorial will teach you how to use HTML_BBCodeParser.
What we will learn today:

- Parsing text
- Adding automatic linebreaks

Parsing Text
Parsing text is easy, using HTML_BBcodeParser!
The code below will demonstrate how to do this. I will explain every step very closely.

<?

/* adjust include_path to include PEAR */
ini_set(&#039;include_path&#039;, ini_get(&#039;include_path&#039;).&#039;:/usr/share/pear&#039;);

/* all your errors are belong to us */
error_reporting(E_ALL);

/* require PEAR and the parser */
require_once(&#039;PEAR.php&#039;);
require_once(&#039;HTML/BBCodeParser.php&#039;);

/* get options from the ini file */
$config = parse_ini_file(&#039;BBCodeParser.ini&#039;, true);
$options = &PEAR::getStaticProperty(&#039;HTML_BBCodeParser&#039;, &#039;_options&#039;);
$options = $config[&#039;HTML_BBCodeParser&#039;];
unset($options);

/* do yer stuff! */
$parser = new HTML_BBCodeParser();
$parser->setText(@$_GET['string']);
$parser->parse();
$parsed = $parser->getParsed();

echo htmlspecialchars striptags( ( $parsed ) );

?>


/* get options from the ini file */
$config = parse_ini_file('BBCodeParser.ini', true);
$options = &PEAR::getStaticProperty('HTML_BBCodeParser', '_options');
$options = $config['HTML_BBCodeParser'];


This loads the configuration file and parses it.
Without the configuration file, HTML_BBCodeParser will not work!

/* do yer stuff! */
$parser = new HTML_BBCodeParser();
$parser->setText(@$_GET['string']);
$parser->parse();
$parsed = $parser->getParsed();


This initializes the class and actually parses the text, stored in $_GET['string'].
Obviously, setText() is used to add text to parse and parse() parses the text stored.
Use getParsed() to make the class return the parsed text.

echo htmlspecialchars( striptags( $parsed ) );

This filters evil HTML chars from the parsed text. Unfortunately HTML_BBCodeParser does not have this for default...
It also outputs the filtered and parsed text.

Adding Linebreaks
Adding linebreaks (changing newlines to breaks ) does not happen automaticly. You have to use the following code:

echo nl2br( htmlspecialchars( striptags( ( $parsed ) ) );


As you might know, nl2br() changes newlines into breaks.

Thank you for reading this tutorial! I hope you actually learnt something and will put the knowledge to good use.
More tutorials coming soon!
Premium Publisher
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Ruben K

i am totally awesome
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top