Single line PHP codes
Posted: Sun Nov 29, 2009 5:10 am
Validate the format of a domain name:
Validate the format of an email address:
Validate the format of the user name and verify valid MX records for the domain name (OK, so it is two lines):
Remove one element from an array (in this example remove $image from array $image_array):
Randomly select of one element out of an array:
Eliminate duplicate (or more) characters in a string (the underscore in the example below):
Include using a full unix path while still maintaining script portability:
Pass the htaccess/htpassword authenticated user to a script:
Set the server time zone (Eastern time in this example):
Debug $_GET variables by displaying the whole search string:
Show the last modified date:
Read a flat file (file.txt in this example) in as an array and remove line breaks:
Scrub html output:
Code: Select all
if (!preg_match("(^([-a-z0-9]+\.)+[a-z]{2,4}$)i", $domain)) echo "Domain name $domain is not valid";
Code: Select all
if (!preg_match("(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i", $email)) echo "Email address $email is not valid";
Code: Select all
list($username, $domaintld) = split("@", $email, 2);
if (!getmxrr($domaintld, $mxrecords) OR !preg_match("(^[-\w\.]+$)", $username)) echo "Email address $email is not valid";
Code: Select all
$image_array = array_merge(array_diff($image_array,(array)$image));
Code: Select all
$image = $image_array[array_rand($image_array)];
Code: Select all
$text = preg_replace('#(_)+#','_',$text);
Code: Select all
include($_SERVER['DOCUMENT_ROOT'] . '/file.inc');
Code: Select all
$user = $_SERVER['REMOTE_USER'];
Code: Select all
putenv('TZ=EST5EDT'); // eastern time
Code: Select all
echo $_SERVER['QUERY_STRING'];
Code: Select all
echo "Last modified: ".date ("F d, Y", getlastmod());
Code: Select all
$newArray = preg_replace("#\r\n?|\n#","",file('file.txt'));
Code: Select all
$output = strtr($output,array('&'=>'&','<'=>'<','>'=>'>'));