Category Archives: fun

Basic REGEX for cleaning and prepping data

Over the last year or so, I have used a PHP Framework Code Igniter, Amongst many of its good features is its built in Form Validation Library.

Every now and then I either write a a small site out of Code Ignite, or help others to write or develop sections of their site, I constantly get MSN/IM messages asking for help which I am happy to help.

More recently I have started compiling a list of the little help functions I have written to generate my own ‘Form Validation’ Class, I hope to post it here soon.

Today though I have been doing a lot of Code Igniter validation, with some extra ‘callbacks’ using regex/preg_match & preg_replace combos,

Note: This is not are Regex tutorial, just some basic common examples.

Basic Examples

Strip non-numeric characters from a string.

< ?php

$string = 'sheldon is not james bond 007';
$numbers = preg_replace('/[^0-9]+/', '', $string);
// returns '007';
?>

Strip all none numeric and alpha characters from a string

$string = 'sheldon, Is james bond (007)';
$numbers = preg_replace('/^([a-z0-9])+$/i' '', $string); // the i means case insensitive.
// returns 'sheldon Is james bond 007';
?>

Create a safe URL string.

want to make url safe permalinks or strings?
lets strip out anything that’ll break our address.

// assume $_POST['name'] = "sheldon's wonderful *product* costs $19.95";
$name = str_replace(' ', '-', trim($_POST['name']));
$url = preg_replace('/^([a-z0-9-_])+$/i', '', $name);

header("Location: http://sheldon.lendrum.co.nz/". $url);
//  http://sheldon.lendrum.co.nz/sheldons-wonderful-product-costs-19.95
?>

preg_replace() & preg_match()

I have used preg_replace() to strip out the unwanted tags, but if you just want to validation against with out altering the users data, you can use preg_match().

Here is an example to make sure a user has only submitted a trough z and 0 trough 9 characters and spaces.

< ?php
// assume $_POST['name'] = "sheldon's wonderful *product* costs $19.95";
if(preg_match('/^([a-z0-9\ ])+$/i',  $_POST['name'])) {
	// valid data ( spaces are allowed. )
	// case insative
	echo('Validation Passed');
} else {
	// this was invalid data
	echo('Please enter a valid string.');
}
?>

In this example, our string would fail.

We allowed spaces in our test, to run the same match with out allowing spaces, re can remove the ‘\ ‘ form the Regex.

Here is an example to make sure a user has only submitted a trough z and 0 trough 0 characters .

< ?php
// assume $_POST['name'] = "sheldon";
if(preg_match('/^([a-z0-9])+$/i',  $_POST['name'])) {
	// valid data ( spaces are  NOT allowed. )
	// case insative
	echo('Validation Passed');
} else {
	// this was invalid data
	echo('Please enter a valid string.');
}
?>

In this example, our post data would pass the validation.

CodeIgniter & SPAW Editor Tabs

A few months ago, I updated a great SPAW editor plug-in for CodeIgniter to use the most recent version of SPAW at the time, that included Safari Browser fixes and some CSS tweeks.

Well over the weekend I look at getting multiple tabs working.
It was pretty easy, with a couple of modifications to the SPAW wrapper class, I have create some sample usage, and examples for you to download.
I have made the CSS fixes from my previous post, and included some sample usage.

SPAW and CodeIgniter

SPAW and CodeIgniter

I am only going to explain how to create the SPAW instance within CodeIgniter, If you are unfamiliar with CI, you should first follow the Online Code Igniter Tutorial Videos.

Creating the SPAW instance

In my download, you will find a ‘pages’ controller.

function index() {

	// Define our first tab
	// 'name' == field name,
	// 'caption' == button caption,
	// 'content' == editor content
	$config = array(
		'name'    => 'content',
		'caption' => 'Content',
		'content' => !empty($_POST['content'])?$_POST['content']:'

CodeIgniter & SPAW Tabs

SPAW Editor Class for CodeIgniter, Sheldon Lendrum

' ); // start our SPAW instance $this->load->library('spaw', $config); // Add our secondary tabs $this->spaw->addTab('description', 'Description', !empty($_POST['description'])?$_POST['description']:''); $this->spaw->addTab('summary', 'Summary', !empty($_POST['summary'])?$_POST['summary']:''); // load our view $this->load->view('template'); }

SPAW Class

I have modified the SPAW class with 3 functions.

function Spaw($config) {}

The Spaw function initiates the Spaw editor. you pass in an array of the default Tab.

function show() {}

This returns the SPAW instance in to your view. It will return a string, so you will need to echo this in your layout.
EG;
< ?php echo($this->spaw->show()); ?>
You must call this ONLY IF SPAW is called in your controller previous.

function addTab($name, $caption, $value) {}

This is to add additional tabs to your spaw instance.
Your default tab is defined when you create your SPAW instance, so this is how you can call additional tabs.
$name == your field/$_POST name
$caption == your button title
$value == the SPAW editor default value.

Installing SPAW & CodeIgniter

I have included a full copy of CodeIgniter 1.7.1 with SPAW 2.0.8.1,
If you want to move the SPAW plugin to your own copy of CI, move the;
'system/application/libraries/spaw2' directory and the
'system/application/libraries/spaw.php' file.

If you are on an APACHE server and using MOD REWRITE url rules, make sure you have the included .htaccess file in the spaw2/ dir.
'system/application/libraries/spaw2/.htaccess'

Download and Install

[download#9]

Facebook Pirate Style

With the Millions of users on Facebook, there is a need for lots of different langauges, but did you know that you can view your facebook in Pirate English! It will change the whole site in to Pirate English, even the notification emails you receive.

Facebook in Pirate English

Facebook in Pirate English

It is Easy to turn on, and easy to turn off once the novelty has worn out.

Enable Pirate English on your Facebook Profile

Log in to your Facebook account, Click the Settings Link in the top Right, Click the Langauge tab, and from the drop down select English (Pirate) Button.

Add me to your Facebook !

BounceBox Update, Version 1.1

I was using an implementation of my BounceBox mootools javascript class on a site I am working on at the moment, and made a couple of additions to the Class that I hope other will enjoy.

The new version1.1 demo can be viewed at http://bouncebox.lendrum.co.nz/ and the change log with a new version 1.1 demo link at the bottom of that page to show full screen positioning that is now supported.

The Demo page has full links to download this free Mootools Javascript effects class.

New Version of 'MiniShowCase Administration' coming – any enhancement requests

I am working on adding some features to the MiniShowCase Administration, like better user management, better image scaling options and better description management.

If anyone has any ideas or enhancement requests, please post a comment below, or to be notified via email when the next version is released, send me an email to: sheldon[at]lendrum.co.nz

Alternating row styles with Javascript

I am writing a list that outputs a large amount of data in a vertical structure, with many rows the list gets hard to tell which entry is which.

Now, normally I would do this via PHP, something like

	< ?php $messages = SQL('QUERY TO GET MY MESSAGES'); ?>
	< ?php $style    = "on"; ?>
	< ?php if($messages): ?>
	

Messages

< ?php foreach($messages as $message): ?>
>

< ?=date('d/m/Y', strtotime($message->last_modified))?> < ?=$message->firstname?> < ?=$message->lastname?>

< ?=$message->message?>

< ?=$message->status?>

< ?php if($style=='on') { $style = 'off' } else { $style = 'on'} ?> < ?php endforeach; ?> < ?php endif; ?>

But I decided today to write it in Javascript. Mootools 1.2 in fact, as the site I am working on had a lot of cool JS effects.

The HTML

	< ?php $messages = SQL('QUERY TO GET MY MESSAGES'); ?>
	< ?php if($messages): ?>
	

Messages

< ?php foreach($messages as $message): ?>

< ?=date('d/m/Y', strtotime($message->last_modified))?> < ?=$message->firstname?> < ?=$message->lastname?>

< ?=$message->message?>

< ?=$message->status?>

< ?php endforeach; ?> < ?php endif; ?>

The Javascript

	$$('.message').each(function(message, row) { // find all elements the have a class called 'message' and loop trough them.
		if(row%2) message.setStyle('background', '#eceff1'); // assign my style to it
	});

You can assign more than one style, by using Mootools setStyles({ }); function. or add events. Eg;

	$$('.message').each(function(message, row) {
		if(row%2) message.setStyle('background', '#eceff1');
		var background = message.getStyle('background');
		message.setStyle('padding', '5px');
		message.addEvents({
			'mouseenter': function() { message.setStyle('background', '#d2d2d2'); },
			'mouseleave': function() { message.setStyle('background', background); }
		});
	});

This will assign a different style and an event back to each element.

Demo:

Hover over any of the below messages :)

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0

23/03/2009
Sheldon Lendrum

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0