Home
Home Page
In detail about Robots.txt
Safe programming on PHP
We write elementary rassylhhik
Use of HTML-tables for a conclusion of diagrams
What is dorvei? Whether Banjat for them?
How to learn{find out} on what searches find my site in search machines?
Cunnings of tabulared design. (we form a framework of the table)
The guest book on PHP
BB-codes
We hide counters
Use of patterns of design in ASP.NET
RSS the generator
Search optimization of a site
Metatags
Optimization for MSN
We
Cookies - fresh rolls
Superdynamical webs - interfaces
Links
 

BB-codes

At once I shall make a reservation, that it is better to make function which will check the text on BB-codes and to give out to his  user. I.e. it is better to store{keep} the data on the server with BB-codes, than with HTML. Though time for delivery of the document will be it is spent little bit more, but you at any time can change style for this or that BB-code. And for this purpose it will not be necessary to touch and change all documents, but only one line in a code.

For the beginning I shall acquaint you with two functions:

str_replace ()


Syntax:



string str_replace (string from, string to, string str)


Function str_replace () replaces in an initial line str one podstroki with others. I.e. function replaces in line str all ocurrences podstroki from on to and returns result. This function can work with binary lines.


Function, generally speaking, necessary. For example, if you write something such as the guest book, a forum, and want, that in the form of input for allocation of the test it was possible to use standard tegami HTML, you can replace symbols which you have chosen for formatting on standard tegi NTML with the help of this function.

For example:



$txt = str_replace ("[B]", "<B>", $txt);


I.e. if you use for display of the text by a semiboldface font symbols "[B]", you should replace them with a symbol "<B>", used in NTML.



preg_replace ()


Syntax:



mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])


This function searches in line subject for conformity to regular expression pattern, and replaces them on replacement. Unessential the parameter limit sets number of conformity with which it is necessary to replace. If this parameter is not specified, or is equal-1 all found conformity are replaced.



<?

$str = " May 15, 2003 ";

$pattern = " / (\w +) (\d +), (\d +)/i ";

$replacement = " 1 \ $ {1} \ $3 ";

print preg_replace ($pattern, $replacement, $str);

?>


Result:



1 May 2003


I think with function str_replace () all understandably, therefore we can proceed{pass} at once to a spelling of the first part of our program:



$text = str_replace ("[b]", "<b>", $text);

$text = str_replace (" [/b] ", " </b> ", $text);


$text = str_replace ("[i]", "<i>", $text);

$text = str_replace (" [/i] ", " </i> ", $text);


$text = str_replace ("[u]", "<u>", $text);

$text = str_replace (" [/u] ", " </u> ", $text);

...


In above resulted code we replace BB-codes with them HTML equivalents. All is good, yes only BB-codes are and it is more complex , for example:



color = * FF0000] Red [/color]


To touch all values of colors (from *000000 up to *FFFFFF), time is required and if to do{make} this check to each document time of loading of the document quickly increases. How then to be? Yes it is very simple, get a grasp of this function more closely{attentively}: ” preg_replace () “.

Who is unfamiliar with regular expressions, I advise to read about it a material.



$search [] = " * \ [color = \ * ([a-f0-9] {6}) \] (. *?) \ [/color \] * si ";

$search [] = " * \ [size = ([1-6] {1}) \] (. *?) \ [/size \] * si ";


$search [] = " * \ [email \] ([a-z0-9\._-] {1},) + ([a-z0-9\._-] {1},) + \. ([a-z] {2,4}) \ [/email \] * si ";

$search [] = " * \ [email = \ “ ([a-z0-9 \ "_-] {1,}) + ([a-z0-9 \ "_-] {1,}) + \ “ ([a-z] {2,4}) \ “ \] (. *?) \ [/email \] * si ";

...


$replace [] = ' <font color = "*\1"> \2 </font> ';

$replace [] = ' <font size = "\1 "> \2 </font> ';


$replace [] = ' <a href = " mailto: //\1 @\2.\3 "> \1 @\2.\3 </a> ';

$replace [] = ' <a href = " mailto:\1 @\2.\3 "> \4 </a> ';

...


$text = preg_replace ($search, $replace, $text);


We assort above the written code. We create two files $search and $replace. In the first regular expression which is searched in the text is written down, and in the second on what it is necessary to replace accordingly.


I think idea be clear, so can make for the site any codes, and not only BB, but can think up still and the. Or to use these:

http: // wackowiki.com/WackoDokumentacija/WackoSintaksis? v=xya