Skip to content

Commit

Permalink
New additions to G-Lib, partially ported over from private repositories.
Browse files Browse the repository at this point in the history
  • Loading branch information
brodkin committed Aug 29, 2011
1 parent 93713a9 commit a8ee894
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 52 deletions.
49 changes: 0 additions & 49 deletions helpers/glib_number_helper.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,5 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

function phone_format($phone = '')
{
// If we have not entered a phone number just return empty
if (empty($phone)) {
return '';
}

// Strip out any extra characters that we do not need only keep letters and numbers
$phone = preg_replace("/[^0-9A-Za-z]/", "", $phone);

// FORMAT
if (strlen($phone) == 10) {
$num = preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1.$2.$3", $phone);
} elseif (strlen($phone) > 10) {
$num = preg_replace("/([0-9a-zA-Z]{1})([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]+)/", "+$1 $2.$3.$4", $phone);
} else $num = $phone;

// RETURN
return $num;

}

function phone_strip ($phone)
{

// REMOVE US (1) PREFIX IF NOT LOCAL EXT
if (strlen($phone) > 4) $phone = ltrim($phone,'1');

// SANITIZE
$phone = preg_replace("/[^0-9A-Za-z\*]/", "", $phone);

// CONVERT LETTERS TO NUMBERS
$replace = array( '2'=>array('a','b','c'),
'3'=>array('d','e','f'),
'4'=>array('g','h','i'),
'5'=>array('j','k','l'),
'6'=>array('m','n','o'),
'7'=>array('p','q','r','s'),
'8'=>array('t','u','v'),
'9'=>array('w','x','y','z'));
// Replace each letter with a number
// Notice this is case insensitive with the str_ireplace instead of str_replace
foreach($replace as $digit=>$letters) {
$phone = str_ireplace($letters, $digit, $phone);
}

return substr($phone, 0, 10);
}

function number_word_format($num, $c=1)
{
// Digits and Periods Only
Expand Down
76 changes: 76 additions & 0 deletions helpers/glib_string_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/***************************************
* NOTES TO SELF:
* tel_convert_vanity and tel_dialtring should be good, but untested. Add support for spaces as punctuation?
* tel_format needs an overhaul.
* phone_format needs to pass data to tel_format and trigger depreciated error.
***************************************/

function phone_format()
{

}

function tel_format($str = '')
{
// If we have not entered a phone number just return empty
if (empty($str)) {
return '';
}

// Strip out any extra characters that we do not need only keep letters and numbers
$str = preg_replace("/[^0-9A-Za-z]/", "", $str);

// FORMAT
if (strlen($str) == 10) {
$num = preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1.$2.$3", $str);
} elseif (strlen($str) > 10) {
$num = preg_replace("/([0-9a-zA-Z]{1})([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]+)/", "+$1 $2.$3.$4", $str);
} else $num = $str;

// RETURN
return $num;

}

function tel_dialtring ($str)
{
$str = tel_convert_vanity($str);

if (preg_match('/^\+[\d]{1,3}[\(\)\d\.a-z]+$/i', $str))
{
return preg_replace('/[^\da-z\+]/i', '', $str);
}
elseif (preg_match('/^[\(]?[2-9]{1}[\d]{2}[\-\)\.]?[\da-z]{3}[\-\.]?[\da-z]{4}$/i', $str))
{
return '+1'.preg_replace('/[^\da-z\+]/i', '', $str);
}
else
{
return $str;
}
}

function tel_convert_vanity ($str)
{
// SANITIZE, ALPHANUM ONLY
$str = preg_replace("/[^0-9A-Za-z\*]/", "", $str);

// CONVERT LETTERS TO NUMBERS
$replace = array( '2'=>array('a','b','c'),
'3'=>array('d','e','f'),
'4'=>array('g','h','i'),
'5'=>array('j','k','l'),
'6'=>array('m','n','o'),
'7'=>array('p','q','r','s'),
'8'=>array('t','u','v'),
'9'=>array('w','x','y','z'));
// Replace each letter with a number
// Notice this is case insensitive with the str_ireplace instead of str_replace
foreach($replace as $digit=>$letters) {
$str = str_ireplace($letters, $digit, $str);
}

return $str;
}
28 changes: 25 additions & 3 deletions helpers/glib_validation_helper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

function is_email($str) {
function is_email($str)
{
$CI =& get_instance();
$CI->form_validation->set_message('is_email', '%s must be a valid email address.');
if (
strlen($str) <= 256
&& preg_match('/([A-Z0-9._%+-]+)@([A-Z0-9.-]+\.[A-Z]{2,4})/i',$str,$part)
Expand All @@ -16,15 +19,34 @@ function is_email($str) {
}
}

function min_value ($val,$min) {
function is_tel($str)
{
if (preg_match('/^\+[\d]{1,3}[\(\)\d\.a-z]+$/i', $str))
{
return true;
}
elseif (preg_match('/^[\(]?[2-9]{1}[\d]{2}[\-\)\.]?[\da-z]{3}[\-\.]?[\da-z]{4}$/i', $str))
{
return true;
}
else
{
$CI->form_validation->set_message('is_tel', '%s must be a valid, 10-digit, U.S. phone number or an international number beginning with a plus sign and country code.');
return false;
}
}

function min_value ($val,$min)
{
settype($min, "float");
$CI =& get_instance();
$CI->form_validation->set_message('min_value', '%s must be at least '.$min.'.');
if ($min <= $val) return TRUE;
else return FALSE;
}

function max_value ($val,$max) {
function max_value ($val,$max)
{
settype($max, "float");
$CI =& get_instance();
$CI->form_validation->set_message('max_value', '%s must not exceed '.$max.'.');
Expand Down

0 comments on commit a8ee894

Please sign in to comment.