Keys →
Functions ↓
function validateTemplate ( &$str, $template, $trunc = false ) {#-k parse string#- $str The string to look for in the template.#- $str can be longer than template; only the length of template is tested#- $template a simple template that must contain the fmt of $str#- In the template, a '9' if it must be a digit, 'a' if it must be a number.#- $trunc setting true will truncate $str to $template length if found#- returns first key of the template array that matches, else -1 if fail#-#- sample template:#- $template = array ( '99999', '9999', 'a9a 9a9', 'a9', 'a99', 'a9a', 'aa9', 'aa99', 'aa9a' );#- given a postcode, will return 0 if US; 1 if AU; 2 if CA; 3 thru 8 if UK#-d 04/06/12 - upgraded $str = trim($str); $sl = strlen($str); if (!$sl) return -1; foreach($template as $key => $t) { if (strlen($str) >= strlen($t)) { for ($i=0;$i<$sl;$i++) { $fail = true; if (($t[$i] == '9') && !is_number($str[$i])) break; elseif (($t[$i] == 'a') && !is_alpha ($str[$i])) break; elseif (($t[$i] == ' ') && ($str[$i] != ' ')) break; $fail = false; } if (!$fail) { if ($trunc) $str = substr($str,0,strlen($t)); return $key; } } } return -1; }?>