PHP-CLi basic functions

PHP-CLi basic functions

The following functions can help you quickly get going with a CLi project.

#!/usr/bin/env php
<?php
 
// Clear the terminal screen.
// Using the passthru function to fun the local system's clear command
// returns none
function clearTerm()
{
        passthru('clear');
}
 
// Short wrapper for fwrite
// Permits easier explicit writing to STDOUT
// returns none
function toSTDOUT($string)
{
        fwrite(STDOUT,$string);
}
 
// Output with default prefix and suffix
function sendOutput($output = '> ',$NL=PHP_EOL)
{
        toSTDOUT($output.$NL);
}
 
// New exit allowing simple debugging via var_dump()
// Otherwise exits normally
// returns none
function nuExit($str=0)
{
        clearTerm();
        if(is_array($str))
        {
                var_dump($str);
                exit(0);
        }
        exit($str.PHP_EOL);
}
 
// get a trimmed string from STDIN
// returns a string
function getSTDIN()
{
        return trim(fgets(STDIN));
}
 
// get input with a prompt
// built in quit catching
// returns a string or nothing
function getInput($prompt = "> ", $silent = FALSE)
{
        if( ! $silent) { sendOutput($prompt); }
        $var = getSTDIN();
 
        if('quit' == $var) nuExit('Quit entered at prompt.');
 
        return $var;
}
 
// Check input with regex
// returns boolean
function checkMatch($needle,$haystack)
{
        if(preg_match('/^'.$needle.'$/i',$haystack)) return TRUE;
        return FALSE;
}

Now that we have those in place, something like the following is easy enough.

#!/usr/bin/env php
<?php
 
// include the functions here
include "functions.file.php";
 
clearTerm();
sendOutput('Hi there, welcome to a simple CLi functions tutorials.');
$response = getInput('Have you done this before?');
if( checkMatch('([Yy][Ee]?(?:[Ss]|[Aa])?)',$response) )
{
        nuExit('Okay, bye!');
}
else
{
        sendOutput('Welcome to PHP-CLi.');
        nuExit();
}

That’s it.

By Mike on December 18, 2014 | PHP, Tutorials
Tags: , , , ,

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: , , ,