Use Output Buffering in PHP

Shortly it is possible to state essence of a problem so: very much often those data which you receive, for example, from a database, should appear in the text of HTML-page much before that site of the program which is engaged in extraction of these data. For example, the heading of page set by a tag <title>, frequently is contents of any field of the table of a DB, and the block of the program which addresses to a DB, is located much after a conclusion of heading of page.


What to do{make}? The answer idle time - to read the documentation. On function ob_ *, since ob_start ()

It appears, that in PHP there is an opportunity not at once to give out result of job of a script to a browser, and to save in the special place called Output Buffer (the buffer of a conclusion). Thus in PHP there are functions which allow to change his  contents. One more argument for ob_start () right at the beginning of the program is the opportunity of installation of various HTTP-headings, for example, cookies, in an any place of a script, instead of in his  most beginning.


By the way, in PHP there is an opportunity "to compress" (zaarkhivirovat`) result of job of your script to reduce quantity{amount} of the traffic walking on a network - for this purpose it is necessary to call function ob_start with parameter "ob_gzhandler": ob_start ("ob_gzhandler"); it is natural, if the browser does not support the compressed content - your page will be transferred{handed} to him without changes.


And now give from words we shall pass to concrete examples:


Attention! It is hereinafter supposed, that right at the beginning of a script buffering of a conclusion is included:

<?

ob_start ();

?>



Example 1: automatic illumination of variables.


Let's assume, you on a site have clauses{articles} devoted PHP. Naturally, they should contain and examples of a code on PHP;) However if all your code will be typed by a font of one color will read this code a little inconveniently. So let's paint a code! Let all names of variables will be dark blue color. And comments - red (we shall consider, that comments begin c //). For this purpose right at the end of a script the following code is added:

<?

$x=ob_get_contents (); // we receive contents output buffer

ob_clean (); // it is cleared it

$x=preg_replace (" / (\\ $ [\\ w] +)/si "," <font color = \ " blue \ "> \\ 1 </font> ", $x);

// We replace everything, that begins with a sign $ and proceeds more,

// Than one alphanumeric symbol

// On the same, but framed by tags <font> dark blue color

$x=preg_replace (" / (? <!:) (\\/\\ / [^ \\ r \\ n] +) [\\ r \\ n]/si "," <font color = \ " red \ "> \\ 1 </font> ", $x);

// We replace comments on themselves, but it is done{made} their red color

// (? <!:) it is necessary casually to not change the link - http://webscript.ru, for example.

echo $x; // result on the person, that is in a browser;)

?>



Naturally, the above-stated example is not ideal - for example, if in your text there will be code JavaScript, in which unintentionally there is a comment to work he will cease. However, with small updating, this example has painted, though and it is not ideal, this clause{article}.


Example 2: automatic recognition URL and generation of links.


Let's assume, you on a site have guest book or a forum, and any visitor has decided to place the link to the resource. To not trouble the user with studying HTML, it would be convenient to make so that all URL, specified by the user, were automatically framed in corresponding links in language HTML. For example:

http://php.net => <a href = " http: // php.net "> php.net </a>

ftp://ftp.chg.ru => <a href = " ftp: // ftp.chg.ru "> ftp.chg.ru </a>



As it would be useful to transform the next lines:

www.webscript.ru => <a href = " http: // www.webscript.ru "> www.webscript.ru </a>

ftp.chg.ru => <a href = " http: // ftp.chg.ru "> ftp.chg.ru </a>



Let's start:


Right at the end of your script it is necessary to add the following code:

<?

$x=ob_get_contents ();

ob_clean ();

$x=preg_replace (" / (? <! \\/) (www \\. [\\ S] +)/si ", ' <a href = " http: // \\ 1 "> \\ 1 </a> ', $x);

// We change everything, that begins with www, but does not follow after/, on <a href = " http: // www... "> www... </a>

// Here it is used "important" regexp - (? <! \\/) - without him  already existing links of type

// <a href = " http: // www... "> too would undergo to transformation...

// The result turns out not absolutely desirable

$x=preg_replace (" / (? <! \\/) (ftp \\. [\\ S] +)/si ", ' <a href = " ftp: // \\ 1 "> \\ 1 </a> ', $x);

// Similarly we act with ftp...

$x=preg_replace (" / (? <! \ ") (http|ftp): \\/\\ / (\\ S +)/si ", ' <a href = " \\ 1: // \\ 2 "> \\ 2 </a> ', $x);

// Here we process lines of a kind (http|ftp): // something, replacing this sequence

// Corresponding tags HTML

echo $x;

?>



Example 3: automatic generation of links to sections of your site


Let's assume, that you are the author of any information resource, and in the materials enough often mention the information which contains in other sections of your site. Agree, it would be convenient, if all names of sections mentioned in the text of clause{article}, were links to corresponding sections. Well so let's make it:

<?

$links=array (

"News" => array ("url" => "/news", "synonyms" => " in section of news, the latest news, news of a site "),

"Votings" => array ("url" => "/votes", "synonyms" => " section of votings, vote ")

);

// The file links is a description of conformity of names of sections and them URL

// It is natural, that on your site he is created from a DB

// Besides for each unit it is useful to have the list of synonyms

// And that php it is not strong in cases, declinations, conjugations, etc.

setlocale (LC_ALL, "ru");

// It is useful to switch on lokal`, and that PHP cannot distinguish capital letters

// Russian from them " brothers smaller ";)

$x=ob_get_contents ();

ob_clean ();

foreach ($links as $word => $ description) {

$synonyms=split (",", $description [' synonyms ']);

// We take the list of synonyms and it is placed them in a file

$synonyms [] = $word;

// The word is a synonym of the

foreach ($synonyms as $replace) {

$x=preg_replace (" / (\\ W) ($replace) (\\ W)/is ", " \\ 1 <a href = \ "". $ description [' url ']. " \ "> \\ 2 </a> \\ 3 ", $x);

// We replace all ocurrences of a synonym with the link to corresponding section

// Saving thus the old name

}

}

echo $x;

?>



Naturally, the above mentioned example cannot be counted final - for example if at you somewhere in synonyms will meet any of the symbols having special value in perl regular expressions, for example, of a bracket result of performance of this slice of the program can become error messages on your pages. Besides above mentioned regular expression does not take into account, that the name of the unit already can be the link or its{her} part, and, accordingly, will spoil her .


But, nevertheless, this technology, with some additions, rescues the author of these lines from a spelling of links to sections of the site. Besides with use output buffering there is very simple a realization enough complex  things - for example, export of the data in excel can be realized, having written it is literally a pair of operators (the help: excel is able to import HTML, well and how to cut off design from the exported table you, probably, already have guessed).