Feb 10th, 2006, 05:48 PM
#1
Thread Starter
<?="Moderator"?>
[PHP] BB Code
This code will match the tags and format them.
Custom tags can be added to the array called $tags, in the format
tag name => style information
eg
'greentext' => 'color: green;'
using the new tag
[greentext]Some text[/greentext]
Heres the code.
PHP Code:
<?
function MATags2($string)
{
$tags = array(
'b' => 'font-weight:bold;',
'u' => 'text-decoration: underline;',
'i' => 'font-style: italic;',
'small' => 'font-size:10px;',
'large' => 'font-size:16px;',
'hb' => 'font-weight:bold;font-size:16px;',
'hu' => 'text-decoration: underline;font-size:16px;',
'hbu' => 'font-weight:bold;text-decoration: underline;font-size:16px;',
'h' => 'font-size:16px;',
'c' => 'text-align:center;',
'l' => 'text-aligb:left;',
'r' => 'text-align:right;');
foreach($tags as $tag =>$val)
{
$list .= $tag . "|";
}
$list = substr($list,0,strlen($list)-1);
$pattern = '#\[('. $list .')\].*?\[\/('. $list .')\]#si';
$match_count = preg_match_all($pattern,$string,$array);
foreach($array[0] as $match)
{
$before_replace = $match;
$after_replace = $match;
$style = "";
$inline = true;
foreach($tags as $tag => $replace)
{
if(preg_match('#\['.$tag.'\]#',$before_replace))
{
$style .= $replace . ' ';
$after_replace = str_replace("[" . $tag . "]","",$after_replace);
$after_replace = str_replace("[/" . $tag . "]","",$after_replace);
if($tag == 'c' || $tag == 'l' || $tag == 'r')
{
$inline = false;
}
}
}
if($inline)
{
$after_replace = "<span style=\"$style\">$after_replace</span>";
}
else
{
$after_replace = "</p><div style=\"$style\">$after_replace</div><p>";
}
$string = str_replace($before_replace,$after_replace,$string);
//clean up any tags no found
}
foreach($tags as $tag => $replace)
{
$string = str_replace("[" . $tag . "]","",$string);
$string = str_replace("[/" . $tag . "]","",$string);
}
return $string;
}
function MAURL($text)
{
$pattern = '#\[(small|large)\].*?\[\/(small|large)\]#si';
if(preg_match_all($pattern,$text,$array))
{
foreach($array[0] as $match)
{
$before_replace = $match;
$after_replace = $before_replace;
$url_pattern = '#\[(email|url)(=.*?|)\](.*?)\[\/(email|url)\]#';
$count = 0;
if(preg_match_all($url_pattern,$after_replace,$url_array))
{
foreach($url_array[0] as $url_match)
{
if(preg_match('/large/',$after_replace))
{
$style = "font-size:16px;";
}
elseif(preg_match('/small/',$after_replace))
{
$style = "font-size:10px;";
}
if($url_array[1][$count] == "url")
{
$after_replace = preg_replace('/\[url\](.*?)\[\/url\]/sim','<a style="' . $style . '" href="\\1">\\1</a>',$after_replace);
$after_replace = preg_replace('/\[url=(.*?)\](.*?)\[\/url\]/sim','<a style="' . $style . '" href="\\1">\\2</a>',$after_replace);
}
elseif($url_array[1][$count] == "email")
{
$after_replace = preg_replace('/\[email\](.*?)\[\/email\]/sim','<a style="' . $style . '" href="mailto:\\1">\\1</a>',$after_replace);
$after_replace = preg_replace('/\[email=(.*?)\](.*?)\[\/email\]/sim','<a style="' . $style . '" href="mailto:\\1">\\2</a>',$after_replace);
}
$text = str_replace($before_replace,$after_replace,$text);
$before_replace = $after_replace;
$count++;
}
}
}
}
return $text;
}
function Formatcode_List($text)
{
$match_count = preg_match_all("#\[list\](.*?)\[/list]#si", $text, $matches);
for ($i = 0; $i < $match_count; $i++){
$before_replace = $matches[1][$i];
$after_replace = $matches[1][$i];
$temp = preg_split('/\[\*\]/',$before_replace);
array_shift($temp);
foreach($temp as $t)
{
$t = trim($t);
if($t != null)
{
$out .= "<li>". nl2br( $t ) . "</li>\n";
}
}
$after_replace = $out;
$str_to_match = "[list]" . $before_replace . "[/list]";
$replacement = $code_start_html;
$replacement .= $after_replace;
$replacement .= $code_end_html;
$text = str_replace($str_to_match,"<ul>" . $replacement . "</ul>", $text);
}
$text = str_replace("[list]", $code_start_html, $text);
$text = str_replace("[/list]", $code_end_html, $text);
return $text;
}
function MATags($text)
{
$text = nl2br($text);
$text = MAURL($text);
$text = MATags2($text);
$text = preg_replace('/\[img=(.*?)\]/sim','<img src="\\1" alt="Article Image"/>',$text);
$text = preg_replace('/\[url\](.*?)\[\/url\]/sim','<a href="\\1">\\1</a>',$text);
$text = preg_replace('/\[url=(.*?)\](.*?)\[\/url\]/sim','<a href="\\1">\\2</a>',$text);
$text = preg_replace('/\[email\](.*?)\[\/email\]/sim','<a href="mailto:\\1">\\1</a>',$text);
$text = preg_replace('/\[email=(.*?)\](.*?)\[\/email\]/sim','<a href="mailto:\\1">\\2</a>',$text);
$text = Formatcode_List($text);
return $text;
}
?>
Here is an example of the code being used: test.php
Heres a list of the tags and what the output it like, without extra CSS being applied to them.
http://www.mindlessant.co.uk/articlecode.php
Attached Files
Last edited by john tindell; May 1st, 2006 at 03:08 PM .
Feb 26th, 2006, 10:05 AM
#2
Junior Member
Re: [PHP] BB Code
hi john,
I have not started to lern PHP yet and just wanted to see what its capible of, so i tryed to download "test.php" and my pc can not reconize the file type ".php" have you any idea how i can veiw the file?.
P.S And do you know any good PHP tutorails esspecialy on installing PHP?
thanks pip
Feb 26th, 2006, 04:24 PM
#3
Thread Starter
<?="Moderator"?>
Re: [PHP] BB Code
Installing PHP, MySql and Apache on Windows written by VisualAd great tutorial to get you started using PHP and MySQL
May 21st, 2006, 07:54 PM
#4
Addicted Member
Re: [PHP] BB Code
You can use notepad, but I recommend this awesome HTML Editor:
Chami HTML-Kit
Mar 12th, 2009, 01:34 PM
#5
Banned
Re: [PHP] BB Code
hi john,
Download the "test.php", thanks to a great, everything works, everything is good.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width