Printed May 25, 2013 from B&T's Tips & Scripts
http://tips-scripts.com
PHP "One Liners"
Validate the format of a domain name:
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:
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 (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";
Remove one element from an array (in this example remove $image from array $image_array):
$image_array = array_merge(array_diff($image_array,(array)$image));
Randomly select of one element out of an array:
$image = $image_array[array_rand($image_array)];
Eliminate duplicate (or more) characters in a string (the underscore in the example below):
$text = preg_replace('#(_)+#','_',$text);
Include using a full unix path while still maintaining script portability:
include($_SERVER['DOCUMENT_ROOT'] . '/file.inc');
Pass the htaccess/htpassword authenticated user to a script:
$user = $_SERVER['REMOTE_USER'];
Set the server time zone (Eastern time in this example):
putenv('TZ=EST5EDT'); // eastern time
Debug $_GET variables by displaying the whole search string:
echo $_SERVER['QUERY_STRING'];
Show the last modified date:
echo "Last modified: ".date ("F d, Y", getlastmod());
Read a flat file (file.txt in this example) in as an array and remove line breaks:
$newArray = preg_replace("#\r\n?|\n#","",file('file.txt'));
Scrub html output:
$output = strtr($output,array('&'=>'&','<'=>'<','>'=>'>'));