Warning, there are not added protection to sql injections. So use mysql_real_escape_string() and htmlspecialchars() Before going though this function!
Though it is recommended you do not use this function for inserting into DB, but for outputting From DB.
sql
Now that we have our DataBase Set up. Lets Connect!
config.php: (name it what you want :P)
Ok now we are done with most db stuff...
Heres the hole bbcode php function.
We need to break that up right? OK!
That just includes the mysql connection file we just made.
This feature is optional. It just tells it to get bbcodes in package 1.
That is a sql query that selects the bbcodes from the DataBase.
Again you can learn sql at the links above.
This sees if there are any bbcodes.
We are now making arrays with strings in php!
You will notice that there is $array['str'] and $array1['preg'].
This is because some codes (Regex) does not get replaced with str_replace right. It needs preg_replace. And it also goes the other way around.
So this just keeps the arrays separate.
Start that loop!
This checks to see what replace type is needed.
Start making that array!
Put the vars to gether to form the array.
This just adds + 1 onto it self making it not 1 anymore.
This makes the array like:
Not:
This is the same thing as:
Just with preg_replace not str_replace.
This is what ends the arrays.
From:
To:
The Coolest part!
This makes the "string" arrays REAL arrays! Cool uh?
The last part! Replace!
Replace with str_replace.
Replace with preg_replace.
Returns the string in it's new form!
BBCODES
Though it is recommended you do not use this function for inserting into DB, but for outputting From DB.
Code:
function my_bbcode($string) { $searches = array( ':)', ':D', ':(', ';)', ':P', '8)' ); $replaces = array( '<img src="bbcodeimgs/default/smile.gif" alt="" />', '<img src="bbcodeimgs/default/biggrin.gif" alt="" />', '<img src="bbcodeimgs/default/sad.gif" alt="" />', '<img src="bbcodeimgs/default/wink.gif" alt="" />', '<img src="bbcodeimgs/default/tongue.gif" alt="" />', '<img src="bbcodeimgs/default/cool.gif" alt="" />' ); $string = str_replace($searches, $replaces, $string); $searches1 = array( '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is', '/\[url\=(.*?)\](.*?)\[\/url\]/is', '/\[color\=(.*?)\](.*?)\[\/color\]/is' ); $replaces1 = array( '<strong>$1</strong>', '<em>$1</em>', '<u>$1</u>', '<a href="$1">$2</a>', '<span style="color: $1;">$2</span>' ); $string = preg_replace($searches1, $replaces1, $string); return $string; }
Code:
CREATE TABLE `site_bbcode` ( `package` tinyint(4) NOT NULL, `search` varchar(255) NOT NULL, `replace` varchar(255) NOT NULL, `replace_type` varchar(10) NOT NULL, `disabled` tinyint(4) NOT NULL );
config.php: (name it what you want :P)
Code:
$db_local = 'localhost'; //Localhost tells the server that the DB is local (dah) $db_user = 'da_user_man'; //This is the user to log into MySql. $db_pass = 'never_give_out_your_pass!'; //This is the pass to log into MySql. $db_name = 'site_bbcode'; //This is the DataBase we are chosing to work with. //Connect to DataBase. mysql_connect($db_local, $db_user, $db_pass) or die(mysql_error());
Heres the hole bbcode php function.
Code:
require 'config.php'; function my_bbcode($string) { //BBcode Out of database!!! FTW!!!! //Get BBcodes From DB $bbcode_package = 1; $bbcodes = mysql_query("SELECT `search`,`replace`,`replace_type` FROM `site_bbcode` WHERE `package`='$bbcode_package' AND `disabled`='0'"); if(mysql_num_rows($bbcodes)!=0) { //Know if it is the first bbcode. $number['str'] = 1; $number['preg'] = 1; //Start array1 $array1['str'] = "\$search['str'] = array("; $array1['preg'] = "\$search['preg'] = array("; //Start array2 $array2['str'] = "\$replace['str'] = array("; $array2['preg'] = "\$replace['preg'] = array("; while($bbcode = mysql_fetch_array($bbcodes)) { if($bbcode['replace_type'] == 'str') { //Make inner array if($number['str'] != 1) { //Not first part (inner) array. $bbcodeA = ", '{$bbcode['search']}'"; $bbcodeB = ", '{$bbcode['replace']}'"; } else { //First part (inner) array. $bbcodeA = "'{$bbcode['search']}'"; $bbcodeB = "'{$bbcode['replace']}'"; } $array1['str'] = $array1['str'].$bbcodeA; $array2['str'] = $array2['str'].$bbcodeB; //Not the first time any more. $number['str']++; } elseif($bbcode['replace_type'] == 'preg') { //Make inner array if($number['preg'] != 1) { //First part (inner) array. $bbcodeA = ", '{$bbcode['search']}'"; $bbcodeB = ", '{$bbcode['replace']}'"; } else { //Not first part (inner) array. $bbcodeA = "'{$bbcode['search']}'"; $bbcodeB = "'{$bbcode['replace']}'"; } $array1['preg'] = $array1['preg'].$bbcodeA; $array2['preg'] = $array2['preg'].$bbcodeB; //Not the first time any more. $number['preg']++; } else { // } } //Finish arrays. $array1['str'] = $array1['str'].");"; $array2['str'] = $array2['str'].");"; $array1['preg'] = $array1['preg'].");"; $array2['preg'] = $array2['preg'].");"; eval($array1['str']); eval($array2['str']); eval($array1['preg']); eval($array2['preg']); $string = str_replace($search['str'], $replace['str'], $string); $string = preg_replace($search['preg'], $replace['preg'], $string); } return $string; }
Code:
require 'config.php';
Code:
$bbcode_package = 1;
Code:
$bbcodes = mysql_query("SELECT `search`,`replace`,`replace_type` FROM `site_bbcode` WHERE `package`='$bbcode_package' AND `disabled`='0'");
Again you can learn sql at the links above.
Code:
if(mysql_num_rows($bbcodes)!=0)
Code:
//No if it is the first bbcode. $number['str'] = 1; $number['preg'] = 1; //Start array1 $array1['str'] = "\$search['str'] = array("; $array1['preg'] = "\$search['preg'] = array("; //Start array2 $array2['str'] = "\$replace['str'] = array("; $array2['preg'] = "\$replace['preg'] = array(";
You will notice that there is $array['str'] and $array1['preg'].
This is because some codes (Regex) does not get replaced with str_replace right. It needs preg_replace. And it also goes the other way around.
So this just keeps the arrays separate.
Code:
while($bbcode = mysql_fetch_array($bbcodes))
Code:
if($bbcode['replace_type'] == 'str')
Code:
if($number['str'] != 1) { //First part (inner) array. $bbcodeA = ", '{$bbcode['search']}'"; $bbcodeB = ", '{$bbcode['replace']}'"; } else { //Not first part (inner) array. $bbcodeA = "'{$bbcode['search']}'"; $bbcodeB = "'{$bbcode['replace']}'"; }
Code:
$array1['preg'] = $array1['preg'].$bbcodeA; $array2['preg'] = $array2['preg'].$bbcodeB;
Code:
$number['str']++; //And $number['preg']++;
This makes the array like:
Code:
array('', '', '', '');
Code:
array(, '', '', '', '');
Code:
elseif($bbcode['replace_type'] == 'preg')
Code:
if($bbcode['replace_type'] == 'str')
Code:
//Finish arrays. $array1['str'] = $array1['str'].");"; $array2['str'] = $array2['str'].");"; $array1['preg'] = $array1['preg'].");"; $array2['preg'] = $array2['preg'].");";
From:
Code:
array('bla', 'da', 'do', 'bla'
Code:
array('bla', 'da', 'do', 'bla');
Code:
eval($array1['str']); eval($array2['str']); eval($array1['preg']); eval($array2['preg']);
The last part! Replace!
Code:
$string = str_replace($search['str'], $replace['str'], $string);
Code:
$string = preg_replace($search['preg'], $replace['preg'], $string);
Code:
return $string;
BBCODES
Code:
INSERT INTO `site_bbcode` (`package`, `search`, `replace`, `replace_type`, `disabled`) VALUES (1, ':)', '<img src="img_smile" alt="" />', 'str', 0), (1, ':D', '<img src="img_bigSmile" alt="" />', 'str', 0), (1, ':(', '<img src="img_sad" alt="" />', 'str', 0), (1, ';)', '<img src="img_wink" alt="" />', 'str', 0), (1, ':P', '<img src="img_tongue" alt="" />', 'str', 0), (1, '8)', '<img src="img_cool" alt="" />', 'str', 0), (1, '/\\[b\\](.*?)\\[\\/b\\]/is', '<strong>$1</strong>', 'preg', 0), (1, '/\\[i\\](.*?)\\[\\/i\\]/is', '<em>$1</em>', 'preg', 0), (1, '/\\[u\\](.*?)\\[\\/u\\]/is', '<u>$1</u>', 'preg', 0), (1, '/\\[url\\=(.*?)\\](.*?)\\[\\/url\\]/is', '<a href="$1">$2</a>', 'preg', 0), (1, '/\\[color\\=(.*?)\\](.*?)\\[\\/color\\]/is', '<span style="color: $1;">$2</span>', 'preg', 0), (1, '/\\[highlight\\](.*?)\\[\\/highlight\\]/is', '<span class="thighlight">$1</span>', 'preg', 0);