Validate numeric string
Posted on May 21, 2008, Filled under PHP,
Bookmark it
Check if a string is numeric or not.
<?php
function validate_number($string)
{
if(is_numeric($string))
{
return true;
}
else
{
return false;
}
}
?>
Posted on May 21, 2008, Filled under PHP,
Bookmark it
Check if a string is numeric or not.
<?php
function validate_number($string)
{
if(is_numeric($string))
{
return true;
}
else
{
return false;
}
}
?>
Posted on May 21, 2008, Filled under PHP,
Bookmark it
This function checks if an email address is valid or not. For example it is very useful if you have to validate a HTML form where someone should enter a real email address.
function ValidateEmail($email)
{
/*
(Name) Letters, Numbers, Dots, Hyphens and Underscores
(@ sign)
(Domain) (with possible subdomain(s) ).
Contains only letters, numbers, dots and hyphens (up to 255 characters)
(. sign)
(Extension) Letters only (up to 10 (can be increased in the future) characters)
*/
$regex = '/([a-z0-9_.-]+)'. # name
'@'. # at
'([a-z0-9.-]+){2,255}'. # domain & possibly subdomains
'.'. # period
'([a-z]+){2,10}/i'; # domain extension
if($email == '') {
return false;
}
else {
$eregi = preg_replace($regex, '', $email);
}
return empty($eregi) ? true : false;
}
$email = 'name@domain.com';
// will return true, since it matches the regex
if(ValidateEmail($email))
{
echo ''.$email.' is a valid e-mail address.';
}
else
{
echo ''.$email.' is not a valid e-mail address.';
}Posted on May 21, 2008, Filled under PHP,
Bookmark it
Using this piece of code you will get the filename of the currently executing script without containing the full path to it.
For instance if $_SERVER['PHP_SELF'] is equal with /public_html/web/login.php our code will return only login.php
$self = basename($_SERVER['PHP_SELF']);
Posted on May 21, 2008, Filled under PHP,
Bookmark it
This function removes empty values from an array. This is useful if you’re working with dynamical arrays (for instance data harvested from a web site).
<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/web-programming/php/remove-empty-values-from-an-array.html
*/
function remove_array_empty_values($array, $remove_null_number = true)
{
$new_array = array();
$null_exceptions = array();
foreach ($array as $key => $value)
{
$value = trim($value);
if($remove_null_number)
{
$null_exceptions[] = '0';
}
if(!in_array($value, $null_exceptions) && $value != "")
{
$new_array[] = $value;
}
}
return $new_array;
}
?>Example of usage:
$array = array("white", "yellow", 0, "green", " ", "navy");
$remove_null_number = true;
$new_array = remove_array_empty_values($array, $remove_null_number);
echo '<pre>'; print_r($new_array); echo '</pre>';The new array will look like this:
Array
(
[0] => white
[1] => yellow
[2] => green
[3] => navy
)NOTE: If you wish to keep the null number (0) then set the variable $remove_null_number to false.
Posted on May 21, 2008, Filled under PHP,
Bookmark it
This function will redirect the visitor to a specific location.
<?php
function go($location)
{
header("Location: ".$location);
exit;
}
// Location
$location = 'http://www.google.com/';
// Redirect the visitor
go($location);
?>