|
|
|
|
|
|
| |
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);
?> | | |
|
| PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | 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 | | | How to use regular expressions to get the list of links from an HTML page Categories : PHP, Regexps, HTML, HTML and 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 | | | 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 | | | Variable serialization and unserialization. Loading and saving variable structures
to and from file. Categories : Arrays, Filesystem, Variables, Strings, PHP | | | Extract keywords from a string having words in " " count as one string. Categories : PHP, Strings, Regexps, Search | | | Simple way of scaling any image to fit either given width or height. Categories : PHP, Graphics, Arrays | | | WWW interface to Unix Manual(phpMan)
Categories : Program Execution, Strings, Arrays, PHP | | | Tweak Array, insert/add elements to any position of your arrays - delete elements from your arrays - move elements within your arrays - replace elements from your arrays ... the array, 'dynamically' grows or shrinks to whatever we tweak it. Categories : PHP Classes, Arrays, PHP | | | A recursive function to traverse a multi-dimensional array where the
dimensions are not known Categories : Arrays, PHP, Algorithms | |
|
|