PHP "One Liners"
Validate the format of a domain name with this one line:
if (!preg_match("(^([-a-z0-9]+\.)+[a-z]{2,4}$)i", $domain)) echo "Domain name $domain is not valid";
Validate the format of an email address format with this one line:
if (!preg_match("(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i", $email)) echo "Email address $email is not valid";
Validate the format of the user name and verify valid MX records for the domain name with this (OK, so it is two lines):
list($username, $domaintld) = split("@", $email, 2);
if (!getmxrr($domaintld, $mxrecords) OR !preg_match("(^[-\w\.]+$)", $username)) echo "Email address $email is not valid";
Eliminate duplicate (or more) characters in a string (the underscore in the example below) with this one line:
$text = preg_replace('#(_)+#','_',$text);
This include uses a full unix path but still maintains script portability:
include($_SERVER['DOCUMENT_ROOT'] . '/file.inc');
Use this to pass the htaccess/htpassword authenticated user to your script:
$user = $_SERVER['REMOTE_USER'];
Set the server time zone with this (Eastern time in this example):
putenv('TZ=EST5EDT'); // eastern time
Debug your $_GET variables by displaying the whole seaerch string with this:
echo $_SERVER['QUERY_STRING'];
Show the last modified date for your webpage with this:
echo "Last modified: " . date ("F d, Y", getlastmod());
You can read a flat file (file.txt in this example) in as an array and remove line breaks with this:
$newArray = preg_replace("#\r\n?|\n#","",file('file.txt'));