Single line PHP codes

Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

Single line PHP codes

Post by Tony » Sun Nov 29, 2009 5:10 am

Validate the format of a domain name:

Code: Select all

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:

Code: Select all

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):

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";  
Remove one element from an array (in this example remove $image from array $image_array):

Code: Select all

$image_array = array_merge(array_diff($image_array,(array)$image));  
Randomly select of one element out of an array:

Code: Select all

$image = $image_array[array_rand($image_array)];  
Eliminate duplicate (or more) characters in a string (the underscore in the example below):

Code: Select all

$text = preg_replace('#(_)+#','_',$text);  
Include using a full unix path while still maintaining script portability:

Code: Select all

include($_SERVER['DOCUMENT_ROOT'] . '/file.inc');  
Pass the htaccess/htpassword authenticated user to a script:

Code: Select all

$user = $_SERVER['REMOTE_USER'];  
Set the server time zone (Eastern time in this example):

Code: Select all

putenv('TZ=EST5EDT'); // eastern time   
Debug $_GET variables by displaying the whole search string:

Code: Select all

echo $_SERVER['QUERY_STRING'];  
Show the last modified date:

Code: Select all

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:

Code: Select all

$newArray = preg_replace("#\r\n?|\n#","",file('file.txt'));  
Scrub html output:

Code: Select all

$output = strtr($output,array('&'=>'&','<'=>'<','>'=>'>')); 
Post Reply

Return to “PHP & MySQL”