//Returns current domain and extension without www
function curdomain()
{
$pageUR1=$_SERVER["HTTP_HOST"];
$curdomain= str_replace("www.", "", $pageUR1);
return $curdomain;
}
Get current domain name
Permanent link to this article: http://sourcecodeexamples.com/php/get-current-domain-name
Get current page URL
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
Permanent link to this article: http://sourcecodeexamples.com/php/get-current-page-url
Age from DATE
This little function returns the age for this DATE (MySQL format)
//Input format: YYYY-MM-DD
function years_age($birthday){
list($year,$month,$day) = explode("-",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($day_diff < 0 || $month_diff < 0)
$year_diff--;
return $year_diff;
}
Permanent link to this article: http://sourcecodeexamples.com/php/age-from-date
Validate JSON format
//Check if a string is json format
function is_json($json) {
if ( preg_match('/^\{[^>]+\}$/', $json) || preg_match('/^\[[^>]+\]$/', $json) ) {
return true;
}
return false;
}
Permanent link to this article: http://sourcecodeexamples.com/php/validate-json-format
Get visitor IP address
//Return ip address of the visitor
function ipadress()
{
return $_SERVER["REMOTE_ADDR"];
}
Permanent link to this article: http://sourcecodeexamples.com/php/get-visitor-ip-address
Generate a random code/password
//Generate a random code function randcode($length ={ $pass = NULL; for($i=0; $i<$length ; $i++) { $char = chr(rand(48,122)); while (!preg_match("/[a-zA-Z0-9]/", $char)) { if($char == $lchar) continue; $char = chr(rand(48,90)); } $pass .= $char; $lchar = $char; } return $pass; }
Permanent link to this article: http://sourcecodeexamples.com/php/generate-a-random-password
PHP Transform string to UTF-8 encoding
Here are some nice pieces of code that allow you to make all your data (i.e. when it comes from datafeeds) consistant in UTF-8
function utf8_transform($content)
{
if(!mb_check_encoding($content, 'UTF-8')
OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {
$content = mb_convert_encoding($content, 'UTF-8');
if (mb_check_encoding($content, 'UTF-8')) {
// log('Converted to UTF-8');
} else {
// log('Could not converted to UTF-8');
}
}
return $content;
}
Another function is this one, that transforms a string so it fits the formats of MySQL better
function utf8_transform_mysql($string)
{
if(empty($string))
{
return $string;
}
$string = preg_match_all("#[\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]#x", $string, $matches );
return implode("",$matches[0]);
}
Permanent link to this article: http://sourcecodeexamples.com/php/transform-string-to-utf-8-encoding
Validate creditcard number
This piece of code will check if a creditcard number could possibly be valid, determined on the number ranges given. It will NOT actually validate the number with the creditcard company but it could function as a pre-check.
function validate_creditcard($number)
{
$false = false;
$card_type = "";
$card_regexes = array(
"/^4\d{12}(\d\d\d){0,1}$/" => "visa",
"/^5[12345]\d{14}$/" => "mastercard",
"/^3[47]\d{13}$/" => "amex",
"/^6011\d{12}$/" => "discover",
"/^30[012345]\d{11}$/" => "diners",
"/^3[68]\d{12}$/" => "diners",
);
foreach ($card_regexes as $regex => $type) {
if (preg_match($regex, $number)) {
$card_type = $type;
break;
}
}
if (!$card_type) {
return $false;
}
$revcode = strrev($number);
$checksum = 0;
for ($i = 0; $i < strlen($revcode); $i++) {
$current_num = intval($revcode[$i]);
if($i & 1) {
$current_num *= 2;
}
$checksum += $current_num % 10; if
($current_num > 9) {
$checksum += 1;
}
}
if ($checksum % 10 == 0) {
return $card_type;
} else {
return $false;
}
}
Permanent link to this article: http://sourcecodeexamples.com/php/validate-creditcard-number
Validate dutch bank account
This is a piece of code used to validate bank account numbers in the Netherlands, and maybe also in other countries. This is also called the ’11-proef’.
function validate_bank($number)
{
$start=0;
$end=9;
for($i = 0; $i < strlen($number); $i++)
{
$num=substr($number,$i,1);
if ( is_numeric( $num ))
{
$start+=$num * $end;
$end--;
}
}
$giro=($end> 1) && ($end< 7);
$mod=$start% 11;
return($giro || !($end || $mod));
}
Permanent link to this article: http://sourcecodeexamples.com/php/validate-dutch-bank-account-number
Basic HTML5 site structure
This is a basic structure you can use for HTML5 pages. Please note, HTML5 is not (yet) supported by all browsers.
<!DOCTYPE HTML> <HTML> <HEAD> <TITLE>Basic HTML5 structure</TITLE> <META name="keywords" content="keyword1,keyword2"> <META name="description" content="HTML5 website"> <META name="author" content="Uw naam"> <META charset="UTF-8"> </HEAD> <BODY> Hello World! </BODY> </HTML>
Permanent link to this article: http://sourcecodeexamples.com/html/basic-html5-site-structure