Preprocessing PHP Includes

The problem, you see, is that I want to include a content file (for example, “content.php”). This file has an array of various texts and randomly outputs one of them. Here’s an example:


$ary = array("test asdf asdf and aasdfadsfa ", " asdf as ak and asdfa sldk a", " asidfu aoi j and a;lskjz.");
echo $ary[rand(0,count($ary)-1)];

Now, we can use include ("content.php"); in our PHP to include that random output on another PHP page, right? What if we wanted to change the word “and” to blue in the text that is output? We could do this in content.php, but if we have many various files like content.php, this would become a tedious process very quickly. Instead, we need to process the output of content.php before including it. So, let’s make a custom include function:

function inc($file){
if(@$handle = fopen($file,'r')){
$data = fread($handle, filesize($file));
fclose($handle);
} else {
return 'Error: Could not open file.';
}
return preg_replace("/\b(and)\b/i", '<span style="color: #00f;">$1</span>', $data);
}

What’s the problem with this? Well, when we read the file in this way, it doesn’t process the PHP in the included file. Uh oh! What do we do? If you’re using PHP5, there’s a really, really quick solution:

function inc($file){
$data = (include $file);
return preg_replace("/\b(and)\b/i", '<span style="color: #00f;">$1</span>', $data);
}

This doesn’t work for PHP4, though, so we have to use this:

function inc($file){
$temp = explode('/', $_SERVER["REQUEST_URI"]);
$temp = $temp[count($temp)-1];
$data = implode('',file('http://'.$_SERVER["SERVER_NAME"].str_replace($temp, 'content.php', $_SERVER["REQUEST_URI"])));
return preg_replace("/\b(and)\b/i", '<span style="color: #00f;">$1</span>', $data);
}

What we’re doing with all this mess is basically getting the full HTTP-address of the file that is being included, and then opening that file as if it were remote (even though it’s on the same server). This makes PHP include the file without outputting it so that we can process it in our own way, and then returns that value. By returning the value (instead of echoing it), we can also run it through other functions. That is, we can say echo str_replace("#00f", "#f00", inc("content.php")); if we want to use red instead of blue.

Newsvine | Del.icio.us | Digg
In Web, PHP on May 27th, 2005 | 2 Remarks

2 Remarks to “Preprocessing PHP Includes”

  1. Dave Child remarks:

    That’s a nice tip. There is another way to do this without using remote files, should the need arise. You can use output buffering - flush the buffer and start a new one before the include, then after the include you can grab the contents of the buffer - which will be the processed include - and assign that to a variable. Empty the buffer and you’ll then have all content before the include sent to the user and the processed include in a variable, which you can then treat like any other normal variable.


    ob_start(); // Starts buffer
    echo 'Hello World';
    ob_flush(); // Flush and empty buffer
    include('foorbar.php'); // Include file
    $include = ob_get_clean(); // Ends buffering and assigns contents of buffer (which is now the result of the processed include) to $include
    $include = preg_replace("/\b(and)\b/i", '$1‘, $include); // And so on ...

  2. Jona remarks:

    Oh, I hadn’t even thought of trying to use the ob-functions. I think I’m going to give it a shot, and see which method is faster — yours definitely looks more clean! Thanks.

Leave a Remark

 

Note: HTML is allowed. (<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> ).