Before you ob_start(), don’t forget ob_flush() and ob_end_clean()…

Before you ob_start(), don’t forget ob_flush() and ob_end_clean()…

Adding compression to your PHP scripts is as easy as:

<?php
// start at the beginning of your output.
ob_start();
 
/*
//////////////////////////////////////////////////
output will be here, whatever it is.
/////////////////////////////////////////////////
*/
 
// and after your output.
ob_flush();
ob_end_clean();
?>

No, really. It is that easy.

By Mike on September 9, 2011 | PHP, Tutorials
Tags: , , ,

PHP Snippet – Article Spinner

I was bored the other day so I made a very simple article spinner…

Takes a sentence like this:
The {quick|fast} {brown|black} {fox|dog|cat} jumps {over|above|around} the {lazy|sleepy} {horse|cow|donkey}.

And turns it into something like:
The fast brown cat jumps above the sleepy donkey.

$content = "The {quick|fast} {brown|black} {fox|dog|cat} jumps 
{over|above|around} the {lazy|sleepy} {horse|cow|donkey}.";
function spinaround($content)
{
    preg_match_all('/{(.*)}/sU',$content,$matches);
    foreach ($matches[0] as $k=>$v)
    {
        $string = $matches[1][$k];
        $new = explode('|',$string);
        $new = $new[array_rand($new)];
        $content = str_replace($v,$new,$content);
    }
return $content;
}
echo spinaround($content);

I know what you’re thinking; “But most article spinners can do two levels of spinning!!” Yeah, well, using regex to do so is a bit of a pain, however using square brackets [ ] this is entirely possible, as follows:

Takes this:
The {quick|fast} {brown|black} {fox|[fat|skinny] dog|cat} jumps {over|above [and beyond|and below]|around} the {[overly|somewhat] lazy|sleepy}
{horse|cow|donkey}.

Turns it into:
The fast brown fox jumps above and beyond the overly lazy horse.

$content = "The {quick|fast} {brown|black} {fox|[fat|skinny] dog|cat} jumps 
{over|above [and beyond|and below]|around} the {[overly|somewhat] lazy|sleepy}
{horse|cow|donkey}.";
function spinaround($content)
{
    preg_match_all('/{(.*)}/sU',$content,$matches);
    foreach ($matches[0] as $k=>$v)
    {
        $string = $matches[1][$k];
        if ( preg_match_all('/\[(.*)\]/sU',$string,$stringmatches) )
        {
            foreach ($stringmatches[0] as $l=>$w)
            {
            $new = explode('|',$stringmatches[1][$l]);
            $new = $new[array_rand($new)];
            $string = str_replace($w,$new,$string);
            }
        }
        $new = explode('|',$string);
        $new = $new[array_rand($new)];
        $content = str_replace($v,$new,$content);
    }
return $content;
}
 
echo spinaround($content);

Now I know the standard ‘spinables’ use { } for both sets of spinning, but oh well.

Enjoy.

By Mike on May 27, 2011 | PHP
Tags: , , ,

PHP Tutorial: The function of functions

Functions in PHP, are wonderful tools. They allow you to go with the DRY (don’t repeat yourself) style of writing code.
Functions are good for when you want to run some code often, perhaps a small block. We’ll make a function called “tax()”, you create a function like this:

<?php
/* our function will have two parameters:
$amount which will be put in by you when you call the function, and
$rate which, by default, will be 0.05, but you can change it anytime */
function tax( $amount, $rate = 0.05 ) {
/* now we need to find out how much the tax will be
 $amount is set when the function is called */
$tax = $rate*$amount;
/* now return the value, for use in your script */
return $tax;
} 
/* now you simply call the function: 
the following code will echo:  5 */
echo tax(100);
 
/* adding a new tax rate 
the following code will echo 8 */
echo tax(100,0.08);
?>

Variables outside a function cannot be used inside a function except for three cases: (to my knowledge)

  • The variable is a super global, such as $_POST, $_GET, $_SESSION, $_ENV etc…
  • The variable is not a variable, but a constant, defined earlier in the execution.
  • The variable is defined in the function using the global ; option.

Using a variable inside a function by definition of global ;

<?php
$fruit = 'Apple';
function fruit() {
    global $fruit;
    echo $fruit;
} 
fruit();
/* However */
function rotten() {
    echo "Rotten $fruit";
} 
/* This will only produce the word Rotten, not Rotten Apple. 
in fact you will get an error! something like this: 
Notice: Undefined variable: fruit in yourfilename.php on line 16 */
rotten();
?>

So that’s the basics of functions, I hope that helps!

By Mike on March 25, 2011 | PHP, Tutorials
Tags: , , ,