Tag Archives: validate

PHP Function to Extract an E-mail from a String Programming 31 MAR 2013

kung-fu-php-logoGiven a string, it would often be quite useful to extract any e-mail addresses that it might contain. Luckily for us, PHP makes this fairly trivial through the use of its powerful filter_var functionality.

Essentially the plan of attack is to take the string, tokenize it by breaking it up on the space character (or whatever other delimiters you wish to use), and then loop through each one of those tokens and check if it matches a recognised email address pattern.

Putting all of this together, we would get a function which looks a little like this:

function email_address_extraction($message){
    foreach(preg_split('/ /', $message) as $token) {
        $email = filter_var(filter_var($token, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
        if ($email !== false) {
            $emails[] = $email;
    return $emails;

To test:

$testmessage = "hello world.  can jon.kabal@image.com, craig.lot+blog@google.com, and roar@hype.com come over   and play?";
$result = email_address_extraction($testmessage);
$testmessage = " craiglot@doe.co.za";
$result = email_address_extraction($testmessage);


How to test if a Crontab is working in Ubuntu CodeUnit 15 JUN 2012

Editing your cron jobs with the crontab -e command will ensure that your crontab is at least syntactically correct, by not installing it unless it is a valid cron file.

Outside of that though, it falls to you to test that your jobs are in fact running correctly. This can be achieved by piping the cron command output to a file that you control for each one of your jobs.

In practice:

00 01 * * * bash mycommand1.sh > /tmp/mycronjob.log 
00 02 * * * bash mycommand2.sh > /tmp/mycronjob.log

And in the same vein, you can check that the crontab as a whole is being executed by creating a job right at the end of the cron list that runs every minute and pipes out its output to a file that you control:

*/1 * * * * echo "Success! $(date)" >> /tmp/cronwatch.log

Note that > means rewrite the target file every time, whilst >> means append to the end of the file (and create if not exist).


Related Link: https://help.ubuntu.com/community/CronHowto

JSONLint – A Good JSON Validator Software & Sites 05 MAR 2012

json-logoJSON, or JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for many languages.

In other words, JSON is a good choice for data to be shuttled back and forth between web services via AJAX thanks to its clean and compact nature.

However, debugging a big chunk of JSON is not always the easiest task, particularly when it appears to be fault. Enter Chris Dary and his fantastic JSON validator and reformatter, JSONLint!

Simply copy or paste your JSON text into the big text box and hit the validate button, and JSONLint will make it look nice and spit out any validation errors it encounters while attempting to parse it. You can also feed it an URL, which it will then take and grab any JSON it detects from its source.

Additionally, if you are looking to squeeze your JSON to as small a form as possible, you can use JSONLint as a JSON compressor if you add ?reformat=compress to the URL.

Excellent free online tool, and well worth checking out if you do a lot of work with JSON.

Related Link: http://jsonlint.com/