/Main_Page

::You must have ninja focus to complete your mission::NinjaFocus::

PHP Patterns

Views:


No, not patterns as in GOF, patterns as in regexes.

Common stuff included in to pretty much all php web applications I write these days. The mighty email address regex was taken from PEAR::Validate::Email. They took it from someone else, anyway this code is made available under the php license.

<?php
/**
 * @package ninja
 * @copyright Kieran Whitbread 2007-2010
 * @license http://www.opensource.org/licenses/php.php PHP License
 */
 
/*
 * Useful regular expressions for checking datatypes. 
 */
define('NINJA_PREG_EMAIL', '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/D');
define('NINJA_PREG_FLOAT', '/^[+-]?(([0-9]+)|([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)|(([0-9]+|([0-9]*\.[0-9]+|[0-9]+\.[0-9]*))[eE][+-]?[0-9]+))$/');
// Yes, this really is the only way to know *for sure* that you have an integer in PHP!
define('NINJA_PREG_INT', '/^[-]?([0-9]|[1-9][0-9]*)$/');
define('NINJA_PREG_UINT', '/^([0-9]|[1-9][0-9]*)$/');


/*
 * Groups / Classes of characters that seem to be used in various RFCs for various
 * reasons.
 */ 
// Definiations from Section 5 of http://www.faqs.org/rfcs/rfc1738.html
define('NINJA_LOWALPHA', 'abcdefghijklmnopqrstuvwxyz');
define('NINJA_HIALPHA', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
define('NINJA_ALPHA', NINJA_LOWALPHA.NINJA_HIALPHA);
define('NINJA_DIGIT', '0123456789');
define('NINJA_SAFE', '$-_.+');
define('NINJA_EXTRA', '!*\'(),');
define('NINJA_NATIONAL', '{}|\\^~[]`');
define('NINJA_PUNCTUATION', '<>#%"');
define('NINJA_RESERVED', ';/?:@&=');
define('NINJA_HEX', NINJA_DIGIT.'abcdefABCDEF');


define('NINJA_TS_OFFSET_28_DAYS', 2419200);
define('NINJA_TS_OFFSET_14_DAYS', 1209600);
define('NINJA_TS_OFFSET_7_DAYS', 604800);
define('NINJA_TS_OFFSET_5_DAYS', 432000);
define('NINJA_TS_OFFSET_24_HOURS', 86400);
define('NINJA_TS_OFFSET_12_HOURS', 43200);
define('NINJA_TS_OFFSET_8_HOURS', 28800);
define('NINJA_TS_OFFSET_1_HOUR', 3600);
define('NINJA_TS_OFFSET_30_MINUTES', 1800);
define('NINJA_TS_OFFSET_20_MINUTES', 1200);
define('NINJA_TS_OFFSET_15_MINUTES', 900);
define('NINJA_TS_OFFSET_10_MINUTES', 600);
define('NINJA_TS_OFFSET_5_MINUTES', 300);

Main Menu

Personal tools

Toolbox