Posted by sarapeter on July 29, 2008
<?Php
function check_email_address($email) {
// First, we check that there’s one @ symbol, and that the lengths are right
if (!ereg(“^[^@]{1,64}@[^@]{1,255}$”, $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode(“@”, $email);
$local_array = explode(“.”, $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg(“^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\”[^(\\|\")]{0,62}\”))$”, $local_array[$i])) {
return false;
}
}
if (!ereg(“^\[?[0-9\.]+\]?$”, $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(“.”, $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg(“^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$”, $domain_array[$i])) {
return false;
}
}
}
return true;
}
?>
Using the function above is relatively simple, as you can see:
<?Php
if (check_email_address($email)) {
echo $email . ‘ is a valid email address.’;
} else {
echo $email . ‘ is not a valid email address.’;
}
?>
Posted in Php Codes, Php Validators | Tagged: Email Address, Email Validator, Php Email | Leave a Comment »
Posted by sarapeter on July 29, 2008
A class for very basic MySQL database connectivity. Written to reduce redundant code in my own projects aswell as aid in debugging and error reporting during the developement phase. Currently connects to a database, execute external files containing SQL commands, insert and update from an array of key => value pairs, insert and update with sql command, query (one result), query (multiple rows), and dump a select query to a table. The zip file contains the class aswell as a demonstration script.
Download Version 1.0.4: db-1.0.4.zip
Posted in Php Database, Php MySql | Tagged: class, Database, free, MySQL, object, oop, PHP, script, source, source code, sql, Web Development | Leave a Comment »
Posted by sarapeter on July 29, 2008
From a function we can get back a set of variables by using an array. A function returns any variable to the main script by using return statement. Here we will try to return a set of variables by using an array. Our main script will receive the array and we will use while each statement to display all elements of an array.
We will change a script a bit and try to pass ( as input ) a string to the function. This string we will break by using split command and create an array. This array we will return to main script for displaying.
<?Php
function test($my_string){
// creating an array by split command
$my_array=split(” “,$my_string);
return $my_array; // returning the array
}
// sending a string to function as input //
$collect_array=test(“Hello welcome to plus2net”);
// displaying the elements of the collected array
while (list ($key, $val) = each ($collect_array)) {
echo “$key -> $val <br>”;
}
?>
Posted in Php Codes | Tagged: Array from function, getting more variables, more than one return, PHP script, Returning Array | Leave a Comment »