WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : BBCode Formatting String
Categories : PHP, HTML, Regexps, Arrays Click here to Update Your Picture
Gerhard Gerome
Date : Mar 19th 2007
Grade : 2 of 5 (graded 2 times)
Viewed : 8024
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Gerhard Gerome
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

Most people are not used to HTML. So if you want to provide formatting option to your comment system or guest book, BBCode is the safest method. BBCode is supported by almost all Bulletin Boards, and Social Networking websites like Orkut. So most of the people are aware of how to use BBCode. Use the function below to add BBCode formatting to your script.

The first argument of the function is the string to be formatted. The second one is optional and setting it to true removes all HTML tags in the string. If it is made false, then the tags will be printed as is.

<?php
function BBCodeFormat($str, $removeTags = FALSE){
   
$str = $removeTags ? strip_tags($str) : htmlentities($str);
   
$str = nl2br($str);
   
$pattern = array(
       
'/\[b\](.*?)\[\/b\]/is',
       
'/\[i\](.*?)\[\/i\]/is',
       
'/\[u\](.*?)\[\/u\]/is',
       
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
       
'/\[url\](.*?)\[\/url\]/is',
       
'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
       
'/\[img\](.*?)\[\/img\]/is',
       
'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
       
'/\[mail\](.*?)\[\/mail\]/is',
       
'/\[font\=(.*?)\](.*?)\[\/font\]/is',
       
'/\[size\=(.*?)\](.*?)\[\/size\]/is',
       
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
    );

   
$replace = array(
         
'<strong>$1</strong>',
       
'<em>$1</em>',
       
'<u>$1</u>',
       
'<a href="$1">$2</a>',
       
'<a href="$1">$1</a>',
       
'<div style="text-align: $1;">$2</div>',
       
'<img src="$1" />',
       
'<a href="mailto:$1">$2</a>',
       
'<a href="mailto:$1">$1</a>',
       
'<span style="font-family: $1;">$2</span>',
       
'<span style="font-size: $1;">$2</span>',
       
'<span style="color: $1;">$2</span>',
    );
   
// Replace Tags
   
$str = preg_replace ($pattern, $replace, $str);
   
// Replace Quotes
   
$open = '<blockquote>';
   
$close = '</blockquote>';
   
preg_match_all ('/\[quote\]/i', $str, $matches);
   
$opentags = count($matches['0']);
   
preg_match_all ('/\[\/quote\]/i', $str, $matches);
   
$closetags = count($matches['0']);
   
$unclosed = $opentags - $closetags;
    for (
$i = 0; $i < $unclosed; $i++)$str .= '</blockquote>';
   
$str = str_replace ('[quote]', $open, $str);
   
$str = str_replace ('[/' . 'quote]', $close, $str);
    return
$str;
}
?>



Example Usage
<?php
$str
= "My [color=red]name[/color] is [u][b]Gerhard[/b] [i]Gerome[/i][/u]";
echo
BBCodeFormat($str);
?>



Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays
columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP
grab the result of any calculation you submit to the Google Calculator.
Categories : PHP, Arrays, Web Services, Regexps, Math.
How to get the source of a site into an array.
Categories : Arrays, HTML, PHP
Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Form is a utility class for generating html forms. It provides form initialization and regex based data validation (both server and client side) with a convenient interface. This version obsoletes version 1.0a
Categories : HTML, PHP, PHP Classes, Regexps
How to use regular expressions to get the list of links from an HTML page
Categories : PHP, Regexps, HTML, HTML and PHP
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
PHP Array to Javascript Object
Categories : PHP, Arrays, Java Script
A simple bubblesort that takes 2 arrays as argument.The first one is the actual data used for sorting, the second is data that will "tag along" with the first array, for instance a descriptive text about the data in the first array.
Categories : Algorithms, Arrays, PHP, Complete Programs
Parse string to find sub-string between two arbitrary strings
Categories : PHP, Strings, HTML and PHP, Arrays
email validator check checker email e-mail email address
Categories : PHP, Email, Regexps
Parse html (title :: meta)
Categories : PHP, HTML and PHP, Regexps