PHP introduced a really nice concept that says: untill the HTML page generation ends data is stored in memory and then it is output all ot once. So, instead of sending your page in slices as it is generated, PHP sends it all at once. But if you make an express request to behave like this!
The great advantage of this method is that you can access your entire HTML before sent. And here, if you got game, possibilities are endless. You can alter your code, change the HTML and even GZip compress your output.
So PHP puts together your page in memory, shoes it to you one last time and then sends it. And when you see you can also touch ;) Best use for this is to cache pages every now and then. Like every one hour you cache your pages to mysql and send them during that one hour from cache and when one hour elapses you cache again and so on. I'll implement that here in a few days!
The learn more about output control go to PHP output control page. There are many functions and tricks but if you grasp those listed here it's as easy as it gets.
The code below is the basic sample. It's the starting point and works like this. You call the ob_start function and use a callback function as parameter. The callback will receive as parameter the HTML of the page right before it is sent. You can then change it or just return it. When your callback function returns the HTML it is output.
Warning: In callback function any call to functions that do an output are disabled. So if you try and echo "something"; you will see it no longer works. Any output has to somehow be appended / inserted in the parameter HTML code and then returned!
The code below is already commented in the use gzip compressions on web pages tutorial. Check that code too and notice the playWithHtml function is both here and there. Use the code there and copy the function from here and have GZip output compression integrated too!
<?
// -------------------------------------------------------------------------------------
// Alter external links here!
function playWithHtml($OutputHtml){
if(!preg_match_all("/<a\s([^>]+)>(.*)<\/a>/Usi",$OutputHtml,$Links))
return $OutputHtml;
$InnerHtmls=$Links[2];
$LinkTags=$Links[1];
foreach($LinkTags as $LinkTag){
if(preg_match("/\srel=/i",$LinkTag) &&
preg_match("/\starget=/i",$LinkTag)
) continue;
// Only add nofollow and target blank on external links!
if(!preg_match("/href=[\"']?http/i",$LinkTag)) continue;
$OldLinkTag=$LinkTag;
if(!preg_match("/\starget=/i",$LinkTag)){
$LinkTag=trim($LinkTag)." target=\"_blank\"";
}
if(!preg_match("/\srel=/i",$LinkTag)){
$LinkTag=trim($LinkTag)." rel=\"nofollow\"";
}
$OutputHtml=str_replace($OldLinkTag,$LinkTag,$OutputHtml);
}
return $OutputHtml;
}
// -------------------------------------------------------------------------------------
function obOutputHandler($OutputHtml){
return playWithHtml($OutputHtml);
}
ob_start("obOutputHandler");
// -------------------------------------------------------------------------------------
?>
<a href="internal.html">the internal ignored link!</a>
<a href="http://www.google.com/">external link!</a>
<a href='http://www.google.com/'>external link!</a>
<a href=http://www.google.com/>external link!</a>
The code above outputs the following HTML. Notice the rel nofollows and target blanks addition to the above HTML.
<a href="internal.html">the internal link!</a>
<a href="http://www.google.com/" target="_blank" rel="nofollow">external link!</a>
<a href='http://www.google.com/' target="_blank" rel="nofollow">external link!</a>
<a href=http://www.google.com/ target="_blank" rel="nofollow">external link!</a>
Hope you enjoy the scripts and use them. And if you like them link to them! Thanks.
Post Feedback