Skip to content

Instantly share code, notes, and snippets.

View jefferyrdavis's full-sized avatar

jeffery Davis jefferyrdavis

View GitHub Profile
$lightgray : #819090;
$gray : #708284;
$mediumgray : #536870;
$darkgray : #475B62;
$darkblue : #0A2933;
$darkerblue : #042029;
$paleryellow : #FCF4DC;
$paleyellow : #EAE3CB;
$yellow : #A57706;
$orange : #BD3613;
@jefferyrdavis
jefferyrdavis / gist:5992291
Last active December 19, 2015 17:38
Snippit: PHP: Target current page (Self-target)
<?php echo $_SERVER['PHP_SELF']; ?>
@jefferyrdavis
jefferyrdavis / gist:5992287
Last active May 16, 2019 06:48
Snippit: PHP: Validate U.S. phone Number
<?php
/*Validates if $phone is a valid U.S. phone number in the 202-555-1234 format.
Note that the first didgit cannot be a 1 as no area code in the U.S. starts with a 1.
*/
function validatePhoneNo($phone) {
if(preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone))
return true;
else
return false;
}
@jefferyrdavis
jefferyrdavis / gist:5992282
Last active January 21, 2020 08:55
Snippit: PHP: Simple US Zip Code format validation
<?php
/* Validates if $zipCode is a 5 digit number in the 12345 format. Note that this simply checks to see if $zipCode is a 5 digit number, not necessarily a valid U.S. Zip Code.
*/
function validateZipCode($zipCode) {
if (preg_match('#[0-9]{5}#', $zipCode))
return true;
else
return false;
}
?>
@jefferyrdavis
jefferyrdavis / gist:5992275
Last active December 19, 2015 17:38
Snippit: PHP: Random String Function
<?php
/* Generates a random string from the charachters listed in $characters and returns the random string.
*/
function genRandomString($length) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%&*()';
$randstring = "";
for ($p = 0; $p < $length; $p++) {
$randstring .= $characters[mt_rand(0, strlen($characters))];
}
return $randstring;
@jefferyrdavis
jefferyrdavis / gist:5992271
Last active December 4, 2020 07:16
Snippit: PHP: Format 10 or 7 digit phone number
<?php
/* formats a 10 or 7 digit phone number (number only) by adding dashes at the appropriate locations */
function phoneFormat($number) {
if(ctype_digit($number) && strlen($number) == 10) {
$number = substr($number, 0, 3) .'-'. substr($number, 3, 3) .'-'. substr($number, 6);
} else {
if(ctype_digit($number) && strlen($number) == 7) {
$number = substr($number, 0, 3) .'-'. substr($number, 3, 4);
}
@jefferyrdavis
jefferyrdavis / gist:5992262
Last active December 19, 2015 17:38
Snippit: PHP: Find the extenstion of a filename
<?php
// used to find the extenstion of a filename
function findExtension ($filename) {
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
?>
@jefferyrdavis
jefferyrdavis / gist:5992247
Created July 13, 2013 21:16
Snippit: PHP: Validate e-mail to RFCs 5321, 5322 and others.
<?php
/*
Use To validate an email address according to RFCs 5321, 5322 and others.
--------------------------------------------------------------------------------
How to use is_email()
--------------------------------------------------------------------------------
1. Add the downloaded file is_email.php to your project
2. In your scripts use it like this:
@jefferyrdavis
jefferyrdavis / gist:5992224
Created July 13, 2013 21:10
Snippit: PHP: Get Client IP Address
<?php
// Used to determine the IP address of the client.
function GetIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
@jefferyrdavis
jefferyrdavis / gist:5992218
Last active September 20, 2022 08:10
Snippit: PHP: Detect User Location by IP (Returns City and State)
<?php
// returns "City, State" if found otherwise defaults to "Unkown Location"
function detect_city($ip) {
$default = 'Unkown Location';
if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
$ip = '8.8.8.8';
$curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';