<%
Response.ContentType = "application/msword"
Response.AddHeader "Content-Disposition", "attachment;filename=nomefile.doc"
response.Write("ciao a tutti che bello questo tutorial lo puoi vedere da <a href="http://www.sastgroup.com">sastgroup.com</a> " & vbnewline)
response.Write("<b>Posso usare codice html</b>")
response.Write ("<div style=""padding:6px; font:12px verdana"">e codice css</span>")
%>
Archivio per January, 2007
Asp non ha una funzione che cripta una stringa. Puoi usare questo codice per criptare una stringa usando MD5. MD5 è un algoritmo di hash. MD5 è veloce e facile da usare.
<%
Dim strString
strString = "Some sample text to test it for MD5"
strString = MD5(strString)
%>
Vedi lo script completo: http://www.dotnetindex.com/articles/3554_How_to_Encrypt_String_Using_MD.asp
Eccovi una guida dettagliata all'installazione di debian linux da fabrizio ciacchi.
Uno dei punti di forza di Debian consiste nella possibilità di essere installata, oltre che dal comunissimo CD-Rom, anche da altri supporti, come penne USB o i più tradizionali dischetti ([www.debian.org/devel/debian-installer]). Proprio quest'ultimo metodo permette di eseguire il netinstall, una modalità di installazione grazie alla quale quasi tutto viene installato con l'ausilio di una connessione internet, scaricando al volo tutto il necessario.
Da qui [http://cdimage.debian.org/pub/weekly] possono essere scaricate le ISO della Sarge (a noi basta il primo CD per poter installare un sistema completo). Come si può notare esistono CD per diverse architetture, quella da scegliere per un normale computer è i386, mentre le altre sono per architetture di computer differenti (ad esempio i Macintosh). I file ".ISO" scaricati devono essere masterizzati; ecco la procedura da seguire con
- Easy CD Creator [www.roxio.com], selezionare File, Record CD from CD image. Cambiare ora il Tipo di file a ISO image file. Cercare poi il file ISO e cliccare su Open. Cliccando poi su Start recording si dà inizio alla scrittura sul CD-R.
- Nero Burning ROM [www.nero.com], selezionare File, Burn CD image. Selezionare il tipo di file a *.* e cercare il file ISO. Le versioni di Nero più vecchie potrebbero non riconoscere il formato, confermare comunque. Nella schermata successiva selezionare i seguenti parametri:
- Type of image: Data Mode 1
- Block size: 2048 bytes
- File precursor and length of the image trailer: 0 bytes
- Scrambled: no
- Swapped: no
Adesso cliccare su OK e Burn
- cdrecord, scrivere semplicemente cdrecord dev=/dev/hdc (dove /dev/hdc è la periferica del masterizzatore) seguito dal path del file ISO (es. /home/utente/immagine.iso).
- K3B [www.k3b.org], selezionare Tools > CD > Burn Image. Selezionare ora l'immagine da masterizzare e cliccare su Start.
Continua a leggere su: http://fabrizio.ciacchi.it/guide.php/sarge.htm
I am going to teach you in depth on creating a basic template engine, this is fairly long so get comfortable
Before we can start we need a .php document lets name it template.engine.php, now we can start coding!
First we need to declare our Template class:
class TemplateEngine // This is our Template Engine class.
{
?>
Remember the class name Template Engine we will to use it later to call the functions.
Now lets move onto the Template Engine’s variables:
var $Template; // Current Templates Name.
?>
Var $Template is the name of the current template we want to process i.e. Index and such, now lets move to the template file extension var.
var $TemplateExt = '.tpl'; // Templates File ext.
?>
Var $TemplateExt is the template files extension we will use .tpl, now lets add the final var we will need the Templates Folder variable:
var $TemplateDir = './templates/'; // Templates Dir.
?>
var $TemplateDir defines the directory with the .tpl files in it, now that we have all the needed variables defined lets move to the function to load the .tpl file for processing:
function SelectTemplateFile($file, $error_line = 0, $error_file = '') // This function will select the requested templates .tpl file.
{
?>
This function we will use to select a .tpl file and prepare it to be processed, $file is the template files, $error_line is the line we are calling this function on, $error_file file is the filename of the file calling the function; this will make it easier to debug if there are any problems.
So lets check to see if the template directory exsits:
if(is_dir($this->TemplateDir)) // If the $templatedir exists.
{
?>
This will check to see if the $TemplateDir var is a directory on the server, if it is we will check to see if the file exists or not, and if it does not the template engine will return an error.
So if the directory exists now let’s check the template file it self, so right now it is set to if the file does not exist lets show an error.
if(!file_exists($this->TemplateDir.$file.$this->TemplateExt)) // If the .tpl file does not exsit.
{
?>
So this will add a few different vars together, $this->TemplateDir the templates directory than after that .$file so the file we want to use, and than it adds the file ext .$this->TemplateExt, so lets move to the error it should display if the file does not exist.
$error = "<b>File:</b> ".$error_file."<br/>
<b>Line:</b> ".$error_line."<br/>
<b>Date:</b> ".date("D M j G:i Y")."<br/><br/>
Error loading ".$this->TemplateDir.$file.$this->TemplateExt.", file does not exist.";
return die($error); // Returns $error.
?>
So this is our error that will display the file the error has occurred in, the errors line and details about the error in this case it will say “Error loading ./Templates/File.tpl”, file does not. Than returns $error as a die statement so nothing else is run.
So if the file does exist lets get it ready to be processed:
}
elseif(file_exists($this->TemplateDir.$file.$this->TemplateExt)) // ElseIf the file exsits.
{
?>
So this will add a few different vars together, $this->TemplateDir the templates directory than after that .$file so the file we want to use, and than it adds the file ext .$this->TemplateExt, now that we know the file exists lets do something with it!
return $this->Template=file_get_contents($this->TemplateDir.$file.$this->TemplateExt); // Returns the current templates data.
} // End ElseIf.
?>
This basically opens up the template file and gets all of its contents, so wait what happens if that directory does not exist. Don’t worry I did not forget about this.
}
elseif(!is_dir($this->TemplateDir)) // ElseIf the directory does not exist.
{
?>
So elseif the template directory does not exist lets die to an error.
$error = "<b>File:</b> ".$error_file."<br/>
<b>Line:</b> ".$error_line."<br/>
<b>Date:</b> ".date("D M j G:i Y")."<br/><br/>
Error opening ".$this->TemplateDir.", directory does not exist.";
return die($error); // Returns $error.
?>
This will return an error with the file, error line and error details, this should say something like Error opening ./Templates, directory does not exist. Great now lets end the elseif and function and move to the next one!
} // End ElseIf.
} // End Function.
?>
Now we need a function to find and replace the tags we have defined, lets start by declaring the function:
function ReplaceVars($vars=array(), $error_line = 0, $error_file = '') // This will replace {var} with a array value.
{
?>
We have 3 vars to this function, the first is an array this is where all the tags will be held, the other 2 are just for debugging purposes.
So lets check if the array actually contains tags for replacement:
if (sizeof($vars) > 0) // If the array of vars is not empty.
{
?>
Using sizeof we can check the size of the array, and if it is Greater than 0 lets begin to replace the tags.
foreach ($vars as $var => $content) // Loops through the array.
{
?>
This foreach will loop through each tag in the array and replace it with its replacement also defined in the array.
Now lets replace the tags with the content:
$this->Template=str_replace("{".$var."}", $content, $this->Template); // Replaces {var} with the array value ($content).
?>
Using str_replace to swap the tags {blah} with actual content Blah in the current template file.
Now end your foreach and lets move to the case that the array is empty:
} // End Foreach.
}
else // Else the array of vars is empty.
{
?>
So this else means that the sizeof is less than 0 meaning it is empty and if it is empty lets show a error.
$error = "<b>File:</b> ".$error_file."<br/>
<b>Line:</b> ".$error_line."<br/>
<b>Date:</b> ".date("D M j G:i Y")."<br/><br/>
Error no tags destined for replacemnt.";
return die($error); // Returns $error.
?>
This will simply display the error file, line and error details this will say “Error no tags destined for replacement.” Returns as a die so the script will stop.
Now end your else and function so we can move to the last part of the class.
} // End Else.
} // End Function.
?>
Now to the final function this will compile the entire template and display it, lets start off by declaring the function:
function Compile() // This function will compile to template and display it.
{
?>
We do not need any variables with this one, because it will use the Template var defined at the very start!
eval("?>".$this->Template."<?"); // This simply allows us to add php into the .tpl file it self, and also returns the finished template.
?>
Using eval instead of just a return will allow us to use php directly inside the tpl file, so end your function and class and lets move to using the template engine!
} // End Function.
} // End Class.
?>
Now lets use this class to parse a template file!
First thing we need to do is include the template.engine.php onto our index.php file:
require_once(“template.engine.php”); // This requires our template.engine.php file so we can call our Template class.
?>
So now we need to call the template engine class:
$TemplateEngine = new TemplateEngine; // This calls our TemplateEngine class.
?>
Great now lets load up a template file:
$TemplateEngine->SelectTemplateFile(‘example’, __LINE__, __FILE__); // This will load example.tpl
?>
So this will use our SelectTemplateFile file and load example.tpl into the class.
$TemplateEngine->ReplaceVars(array(
'name' => 'Fred',
'message' => 'this is an example on using a basic template engine<br/>pretty sweet huh?'
), __LINE__, __FILE__);
?>
This will replace each tag with content, we hold the data using an array, so example we have a tag on the template called {name} so we define it on the array as name and the replacement content as Fred, and we also have a {message} tag to be replaced. You can add as many tags as you wish, the tag content can also be from a MySQL table so instead of Fred you would put like $r[‘name’] or what ever!
Now call your Compile function and you are done!
$TemplateEngine->Compile();
?>
Now lets make that .tpl file!
Create a file called example.tpl and place it in your templates directory:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Template Engine Test</title>
</head>
<body>
Hello {name}!
{message}
</body>
</html>
See the {name} and {message} tags, these will be replaced using our ReplaceVars function, if you want to add new tags just simply define them on the .tpl file {tag} and in the array add a new tagname and replacement “tag” => ‘Replace Text’, and you now have a simple Template Engine!
Here is the full code for all you copy and pastors:
template.engine.php
class TemplateEngine // This is our TemplateEngine class.
{
var $Template; // Current Templates Name.
var $TemplateExt = '.tpl'; // Templates File ext.
var $TemplateDir = './templates/'; // Templates Dir.
function SelectTemplateFile($file, $error_line = 0, $error_file = '') // This function will select the requested templates .tpl file.
{
if(is_dir($this->TemplateDir)) // If the $templatedir exsits.
{
if(!file_exists($this->TemplateDir.$file.$this->TemplateExt)) // If the .tpl file does not exsit.
{
$error = "<b>File:</b> ".$error_file."<br/>
<b>Line:</b> ".$error_line."<br/>
<b>Date:</b> ".date("D M j G:i Y")."<br/><br/>
Error loading ".$this->TemplateDir.$file.$this->TemplateExt.", file does not exist.";
return die($error); // Returns $error.
}
elseif(file_exists($this->TemplateDir.$file.$this->TemplateExt)) // ElseIf the file exsits.
{
return $this->Template=file_get_contents($this->TemplateDir.$file.$this->TemplateExt); // Returns the current templates data.
} // End ElseIf.
}
elseif(!is_dir($this->TemplateDir)) // ElseIf the directory does not exsit.
{
$error = "<b>File:</b> ".$error_file."<br/>
<b>Line:</b> ".$error_line."<br/>
<b>Date:</b> ".date("D M j G:i Y")."<br/><br/>
Error opening ".$this->TemplateDir.", directory does not exist.";
return die($error); // Returns $error.
} // End ElseIf.
} // End Function.
function ReplaceVars($vars=array(), $error_line = 0, $error_file = '') // This will replace {var} with a array value.
{
if (sizeof($vars) > 0) // If the array of vars is not emtpy.
{
foreach ($vars as $var => $content) // Loops through the array.
{
$this->Template=str_replace("{".$var."}", $content, $this->Template); // Replaces {var} with the array value ($content).
} // End Foreach.
}
else // Else the array of vars is empty.
{
$error = "<b>File:</b> ".$error_file."<br/>
<b>Line:</b> ".$error_line."<br/>
<b>Date:</b> ".date("D M j G:i Y")."<br/><br/>
Error no tags destined for replacemnt.";
return die($error); // Returns $error.
} // End Else.
} // End Function.
function Compile() // This function will compile to template and display it.
{
eval("?>".$this->Template."<?"); // This simply allows us to add php into the .tpl file it self, and also returns the finished template.
} // End Function.
} // End Class.
?>
index.php
require_once('template.engine.php');
$TemplateEngine = new TemplateEngine;
$TemplateEngine->SelectTemplateFile('example', __LINE__, __FILE__);
$TemplateEngine->ReplaceVars(array(
'name' => 'Fred',
'message' => 'this is an example on using a basic template engine<br/>pretty sweet huh?'
), __LINE__, __FILE__);
$TemplateEngine->Compile();
?>
example.tpl
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Template Engine Test</title>
</head>
<body>
Hello {name}!
{message}
</body>
</html>
And that’s that
Link: http://deadman2.com/Tutorials/PHP/Creating%20a%20Basic%20Template%20Engine.lawl
This part will cover converting Htmlspeicalchars back into there original form, as well as protecting your bbcode tags from Unwanted-JavaScript.
So this is your scenario, you are using htmlspeicalchars to stop html and other code from being posted into your comments, but you are using code tags and you don't want that to be filtered so you want to convert your htmlsepcialschars back to there original values.
First method will only work with PHP 5 >= 5.1.0RC1 and that method is using htmlspecialchars_decode().
Second method will work with PHP 4 and PHP 5, this will use get_html_translation_table, array_flip and strtr.
So now we will take our bbcode function and create a code tag, but instead of adding it to our arrays we shall create an anonymous function using create function and also we will use preg_replace_callback.
<?php
$string = preg_replace_callback('#\[code\](.*?)\[\/code\]#s', create_function('$matches', ""), $string); // This is our preg_replace_callback function, this is for our code tag.
?>
So once you have that we will need to build the annoymus function.
<?php
$string = preg_replace_callback('#\[code\](.*?)\[\/code\]#s', create_function('$matches', "
\$string = \$matches[0];
" ), $string); // This is our preg_replace_callback function, this is for our code tag.
?>
$string just defines our string as $matches[0] and $matches[0] is just what's between the code tags. Please take note, I am using double quotes for this function and double quote with this function requires all variables to be back-slashed example: $test would be /$test.
So now you need to use str_replace to replace the two code tags so they aren't there in the end.
<?php
$string = preg_replace_callback('#\[code\](.*?)\[\/code\]#s', create_function('$matches', "
\$string = \$matches[0];
\$string = str_replace('[code]', '', \$string);
\$string = str_replace('[/code]', '', \$string);
" ), $string); // This is our preg_replace_callback function, this is for our code tag.
?>
Now that the functions removes the code tag from the string, let's add a div and place the code inside it.
<?php
$string = preg_replace_callback('#\[code\](.*?)\[\/code\]#s', create_function('$matches', "
\$string = \$matches[0];
\$string = str_replace('[code]', '', \$string);
\$string = str_replace('[/code]', '', \$string);
\$string = '<div>' . \$string . '</div>';
" ), $string); // This is our preg_replace_callback function, this is for our code tag.
?>
Now that will add a div around the code, you can style it later if you want ![]()
Wait a second… I said I would teach you how to change htmlspeicalchars back to there origioals now didn't I?
Well before we finish off that code function we need to create a new function and call it UndoHtmlspecialchars.
<?php
function UndoHtmlspecialchars($string) // UndoHtmlspecialchars, converts htmlspecialchars back to there originals.
{
} // End Function.
?>
Now that the function has been created lets do a few things and return the string in one go.
First we need to use strtr, witch translates certain characters.
<?php
function UndoHtmlspecialchars($string) // UndoHtmlspecialchars, converts htmlspecialchars back to there originals.
{
return strtr($string, );
} // End Function.
?>
Now that we have strtr, we need to use array_flip and get_html_translation_table.
<?php
function UndoHtmlspecialchars($string) // UndoHtmlspecialchars, converts htmlspecialchars back to there originals.
{
return strtr($string, array_flip(get_html_translation_table()));
} // End Function.
?>
So array_flip will flip the order of the get_html_translation_table, so than strtr swaps all the <'s and such with there originals.
Once your function is complete we can move back to the code tag, now remember at the very start of anonymous function I had \\$string = \\$matches[0]? Now we need to wrap this new function called UndoHtmlspecialchars around it, if you have php5 you don't have to use this function you can just use htmlspecialchars_decode.
PHP 4
<?php
$string = preg_replace_callback('#\[code\](.*?)\[\/code\]#s', create_function('$matches', "
\$string = UndoHtmlspecialchars(\$matches[0]);
\$string = str_replace('[code]', '', \$string);
\$string = str_replace('[/code]', '', \$string); \$string = str_replace('[/code]', '', \$string);
\$string = '<div>\$string</div>';
" ), $string); // This is our preg_replace_callback function, this is for our code tag.
?>
PHP 5
<?php
$string = preg_replace_callback('#\[code\](.*?)\[\/code\]#s', create_function('$matches', "
\$string = htmlspecialchars_decode(\$matches[0]);
\$string = str_replace('[code]', '', \$string);
\$string = str_replace('[/code]', '', \$string);
\$string = '<div>\$string</div>';
" ), $string); // This is our preg_replace_callback function, this is for our code tag.
?>
And there you go, now your code tags will replace htmlspeicalchars with there originals, now just return the $string and your code tag is done ![]()
<?php
$string = preg_replace_callback('#\[code\](.*?)\[\/code\]#s', create_function('$matches', "
\$string = UndoHtmlspecialchars(\$matches[0]);
\$string = str_replace('[code]', '', \$string);
\$string = str_replace('[/code]', '', \$string);
\$string = '<div>\$string</div>';
return \$string;
" ), $string); // This is our preg_replace_callback function, this is for our code tag.
?>
Link: http://deadman2.com/Tutorials/PHP/Creating%20Bulletin%20Board%20Code%20Part%202.lawl
Part 1 of this 2 part tutorial will cover just the basic tags such as bold, italic, underlined, image, url, and a bunch of others!
So lets create our bbcode.php file, this will contain our bbcode function. Lets call this function “ParseBB”.
<?php
function ParseBB($string) // This is our ParseBB function.
{
} // End Function.
?>
Now that you have your ParseBB function lets move onto the first step in any bbcode, nl2br(), this is a very important function because it will convert new lines into html line breaks.
<?php
function ParseBB($string) // This is our ParseBB function.
{
$string = nl2br($string); // This will convert all new lines to html line breaks.
} // End Function.
?>
Now that your new lines are now converted lets define our two arrays, $match and $replace. The array $match will store all the bbcode tags and $replace will contain all their counter parts (the html).
<?php
function ParseBB($string) // This is our ParseBB function.
{
$string = nl2br($string); // This will convert all new lines to html line breaks.
$match = array(); // This is our $match array, it will hold all the bbcode tags.
$replace = array(); // This is our $replace array, it will hold all the html replacements.
} // End Function.
?>
Once you have added the arrays lets add some data into them, the first bbcode tag we will add is Bold.
<?php
function ParseBB($string) // This is our ParseBB function.
{
$string = nl2br($string); // This will convert all new lines to html line breaks.
$match = array( // This is our $match array, it will hold all the bbcode tags.
'#\[b\](.*?)\[\/b\]#se', // So this is the bold tag.
); // This is our $match array, it will hold all the bbcode tags.
$replace = array( // This is our $replace array, it will hold all the html replacements.
"'<b>\\1</b>'", // This is what the bold tag is replaced with.
); // This is our $replace array, it will hold all the html replacements.
} // End Function.
?>
So goahead and add a few of your own tags!
It’s simple all you have to do is switch the b for another letter/word and the replacement for your html replacement. Here ill do a few for you!
<?php
function ParseBB($string) // This is our ParseBB function.
{
$string = nl2br($string); // This will convert all new lines to html line breaks.
$match = array( // This is our $match array, it will hold all the bbcode tags.
'#\[b\](.*?)\[\/b\]#se', // So this is the bold tag.
'#\[i\](.*?)\[\/i\]#se', // So this is the italics tag.
'#\[u\](.*?)\[\/u\]#se', // So this is the underlined tag.
'#\[s\](.*?)\[\/s\]#se', // So this is the strike tag.
); // This is our $match array, it will hold all the bbcode tags.
$replace = array( // This is our $replace array, it will hold all the html replacements.
"'<b>\\1</b>'", // This is what the bold tag is replaced with.
"'<i>\\1</i>'", // This is what the italics tag is replaced with.
"'<u>\\1</u>'", // This is what the underlined tag is replaced with.
"'<strike>\\1</strike>'", // This is what the strike tag is replaced with.
); // This is our $replace array, it will hold all the html replacements.
} // End Function.
?>
Pretty simple eh?
Try some on your own!
Now let’s add some URL tags, we will add two kinds one where it just creates a link and one where the user can specify the link.
<?php
function ParseBB($string) // This is our ParseBB function.
{
$string = nl2br($string); // This will convert all new lines to html line breaks.
$match = array( // This is our $match array, it will hold all the bbcode tags.
'#\[b\](.*?)\[\/b\]#se', // So this is the bold tag.
'#\[i\](.*?)\[\/i\]#se', // So this is the italics tag.
'#\[u\](.*?)\[\/u\]#se', // So this is the underlined tag.
'#\[s\](.*?)\[\/s\]#se', // So this is the strike tag.
'#\[url=(.*?)\](.*?)\[\/url\]#se', // This url tag will let a user input a title for the link as well as the links location.
'#\[url\](.*?)\[\/url\]#se', // This url tag will just use the url’s location as the title.
); // This is our $match array, it will hold all the bbcode tags.
$replace = array( // This is our $replace array, it will hold all the html replacements.
"'<b>\\1</b>'", // This is what the bold tag is replaced with.
"'<i>\\1</i>'", // This is what the italics tag is replaced with.
"'<u>\\1</u>'", // This is what the underlined tag is replaced with.
"'<strike>\\1</strike>'", // This is what the strike tag is replaced with.
"'<a href=\"\\1\" target=\"_BLANK\">\\2</a>'", // This is what the first url tag is replaced with, notice I have \\1 and \\2 that’s because there are two (.*?) witch is just a wild card.
"'<a href=\"\\1\" target=\"_BLANK\">\\1</a>'", // This is what the second url tag is replaced with, notice this time there is only \\1.
); // This is our $replace array, it will hold all the html replacements.
} // End Function.
?>
Wasn’t that easy?
Let’s add a email and image tag next!
<?php
function ParseBB($string) // This is our ParseBB function.
{
$string = nl2br($string); // This will convert all new lines to html line breaks.
$match = array( // This is our $match array, it will hold all the bbcode tags.
'#\[b\](.*?)\[\/b\]#se', // So this is the bold tag.
'#\[i\](.*?)\[\/i\]#se', // So this is the italics tag.
'#\[u\](.*?)\[\/u\]#se', // So this is the underlined tag.
'#\[s\](.*?)\[\/s\]#se', // So this is the strike tag.
'#\[url=(.*?)\](.*?)\[\/url\]#se', // This url tag will let a user input a title for the link as well as the links location.
'#\[url](.*?)\[\/url\]#se', // This url tag will just use the url’s location as the title.
'#\[email\](.*?)\[\/email\]#se', // This is the email tag it will change a email address into a mailto link.
'#\[img\](.*?)\[\/img\]#se', // This is the image tag it allows the user to embed an image into there post / content / whatever.
); // This is our $match array, it will hold all the bbcode tags.
$replace = array( // This is our $replace array, it will hold all the html replacements.
"'<b>\\1</b>'", // This is what the bold tag is replaced with.
"'<i>\\1</i>'", // This is what the italics tag is replaced with.
"'<u>\\1</u>'", // This is what the underlined tag is replaced with.
"'<strike>\\1</strike>'", // This is what the strike tag is replaced with.
"'<a href=\"\\1\" target=\"_BLANK\">\\2</a>'", // This is what the first url tag is replaced with, notice I have \\1 and \\2 that’s because there are two (.*?) witch is just a wild card.
"'<a href=\"\\1\" target=\"_BLANK\">\\1</a>'", // This is what the second url tag is replaced with, notice this time there is only \\1.
"'<a href=\"mailto:\\1\" target=\"_BLANK\">\\1</a>'", // This is what the email tag is replaced with, its similar to the url tag but instead of just \\1 for the source it has mailto: inforont of the \\1.
"'<img border=\"0\" src=\"\\1\" alt=\"lol\" //>'", // This will replace the image tag, if the image is 404 it will say Image Not Found along with the original image url.
); // This is our $replace array, it will hold all the html replacements.
} // End Function.
?>
Now that’s quite a few bbcode tags isn’t it?
But what kind of bbcode function would this be without emoticons?
I shall define four emoticons, you can add more if you want ![]()
<?php
function ParseBB($string) // This is our ParseBB function.
{
$string = nl2br($string); // This will convert all new lines to html line breaks.
$match = array( // This is our $match array, it will hold all the bbcode tags.
'#\[b\](.*?)\[\/b\]#se', // So this is the bold tag.
'#\[i\](.*?)\[\/i\]#se', // So this is the italics tag.
'#\[u\](.*?)\[\/u\]#se', // So this is the underlined tag.
'#\[s\](.*?)\[\/s\]#se', // So this is the strike tag.
'#\[url=(.*?)\](.*?)\[\/url\]#se', // This url tag will let a user input a title for the link as well as the links location.
'#\[url](.*?)\[\/url\]#se', // This url tag will just use the url’s location as the title.
'#\[email\](.*?)\[\/email\]#se', // This is the email tag it will change a email address into a mailto link.
'#\[img](.*?)\[\/img\]#se', // This is the image tag it allows the user to embed an image into there post / content / whatever.
'#:\)#se', // This is our smile emoticon, take note of how to escaped the ).
'#:O#se', // This is our omg emoticons, take note of how I did not have to escape the O, you have to escape the ) but not O.
); // This is our $match array, it will hold all the bbcode tags.
$replace = array( // This is our $replace array, it will hold all the html replacements.
"'<b>\\1</b>'", // This is what the bold tag is replaced with.
"'<i>\\1</i>'", // This is what the italics tag is replaced with.
"'<u>\\1</u>'", // This is what the underlined tag is replaced with.
"'<strike>\\1</strike>'", // This is what the strike tag is replaced with.
"'<a href=\"\\1\" target=\"_BLANK\">\\2</a>'", // This is what the first url tag is replaced with, notice I have \\1 and \\2 that’s because there are two (.*?) witch is just a wild card.
"'<a href=\"\\1\" target=\"_BLANK\">\\1</a>'", // This is what the second url tag is replaced with, notice this time there is only \\1.
"'<a href=\"mailto:\\1\" target=\"_BLANK\">\\1</a>'", // This is what the email tag is replaced with, its similar to the url tag but instead of just \\1 for the source it has mailto: inforont of the \\1.
"'<img border=\"0\" src=\"\\1\" alt=\"lol\" />'", // This will replace the image tag, if the image is 404 it will say Image Not Found along with the original image url.
"'<img border=\"0\" src=\"URL TO EMOTICON\" alt=\"emote\" />'", // This is what the smile emoticon tag will be replaced with, make sure to change the URL TO EMOTICON to your URL.
"'<img border=\"0\" src=\" URL TO EMOTICON\" alt=\"emote\" />'", // This is what the omg emoticon tag will be replaced with, make sure to change the URL TO EMOTICON to your URL.
); // This is our $replace array, it will hold all the html replacements.
} // End Function.
?>
Now that you have your two arrays, lets use preg_replace to replace the $match with $replace in our $string.
<?php
function ParseBB($string) // This is our ParseBB function.
{
$string = nl2br($string); // This will convert all new lines to html line breaks.
$match = array( // This is our $match array, it will hold all the bbcode tags.
'#\[b\](.*?)\[\/b\]#se', // So this is the bold tag.
'#\[i\](.*?)\[\/i\]#se', // So this is the italics tag.
'#\[u\](.*?)\[\/u\]#se', // So this is the underlined tag.
'#\[s\](.*?)\[\/s\]#se', // So this is the strike tag.
'#\[url=(.*?)\](.*?)\[\/url\]#se', // This url tag will let a user input a title for the link as well as the links location.
'#\[url](.*?)\[\/url\]#se', // This url tag will just use the url’s location as the title.
'#\[email\](.*?)\[\/email\]#se', // This is the email tag it will change a email address into a mailto link.
'#\[img](.*?)\[\/img\]#se', // This is the image tag it allows the user to embed an image into there post / content / whatever.
'#:\)#se', // This is our smile emoticon, take note of how to escaped the ).
'#:O#se', // This is our omg emoticons, take note of how I did not have to escape the O, you have to escape the ) but not O.
); // This is our $match array, it will hold all the bbcode tags.
$replace = array( // This is our $replace array, it will hold all the html replacements.
"'<b>\\1</b>'", // This is what the bold tag is replaced with.
"'<i>\\1</i>'", // This is what the italics tag is replaced with.
"'<u>\\1</u>'", // This is what the underlined tag is replaced with.
"'<strike>\\1</strike>'", // This is what the strike tag is replaced with.
"'<a href=\"\\1\" target=\"_BLANK\">\\2</a>'", // This is what the first url tag is replaced with, notice I have \\1 and \\2 that’s because there are two (.*?) witch is just a wild card.
"'<a href=\"\\1\" target=\"_BLANK\">\\1</a>'", // This is what the second url tag is replaced with, notice this time there is only \\1.
&
