Code snippets
Here are some small pieces of code I have done either out of curiosity or simply because I needed it. Most of these codes will seem very basic and easy to advanced PHPers.Negative number to positive & vice versa
Changing a positive number into a negative and vice versa can be done in one line:function change_pol($in){
return (0 - $in);
}
Examples:return (0 - $in);
}
echo change_pol(1.5); //outputs -1.5
echo change_pol('-2.'); //outputs 2
echo change_pol(0); //outputs 0
echo change_pol(-5 * -34 * 1.98); //outputs -336.6
$test = "-.8";
$test = change_pol($test);
echo $test; //outputs 0.8
echo change_pol('-2.'); //outputs 2
echo change_pol(0); //outputs 0
echo change_pol(-5 * -34 * 1.98); //outputs -336.6
$test = "-.8";
$test = change_pol($test);
echo $test; //outputs 0.8
If you are using values which cannot be interpreted as numeric values (e.g. change_pol('abc')), 0 will be returned. Even at an error_reporting level of E_ALL, no error is outputted.
GMail address
GMail.Com e-mail addresses can be written in different forms. gmail@gmail.com is the same as gm.ail@googlemail.com, gmAI.l@gmail.com, G.Mail@googlemail.com etc. To prevent users from using the same e-mail address in different variations, put the Gmail address through this code before storing it:function GMailID($mail){
//see: http://bit.ly/GmailID
$mail = strtolower($mail);
$mail = explode('@', $mail);
if($mail[1] != 'gmail.com' && $mail[1] != 'googlemail.com'){
return implode('@', $mail);
}
$mail[0] = str_replace('.', '', $mail[0]);
return $mail[0].'@gmail.com';
}
//see: http://bit.ly/GmailID
$mail = strtolower($mail);
$mail = explode('@', $mail);
if($mail[1] != 'gmail.com' && $mail[1] != 'googlemail.com'){
return implode('@', $mail);
}
$mail[0] = str_replace('.', '', $mail[0]);
return $mail[0].'@gmail.com';
}
Please be advised that this function might behave oddly or throw error messages if you supply an invalid e-mail address. Please check the input before passing it to GMailID().
Examples of usage:
echo GmailID("G.Mail@GOOGLEmail.com"); //outputs gmail@gmail.com
echo GMailID("GM.ail@GMail.Com"); //outputs gmail@gmail.com
echo GMailID("G.Mail@hotMail.com"); //outputs g.mail@hotmail.com
echo GMailID("GM.ail@GMail.Com"); //outputs gmail@gmail.com
echo GMailID("G.Mail@hotMail.com"); //outputs g.mail@hotmail.com
Highlight PHP
This highlights some PHP code, without the need of the wrapping <?php and ?> tags. (This very function is used on this page.)function CodeHigh($str){
$code = highlight_string('<?php '.$str.'?'.'>', true);
if(substr($code, 86, 2) != '<s'){
$code = '<span style="color: #0000BB">'.substr($code, 79, -57);
}
else{
$code = substr($code, 86, -57);
}
echo $code;
}
$code = highlight_string('<?php '.$str.'?'.'>', true);
if(substr($code, 86, 2) != '<s'){
$code = '<span style="color: #0000BB">'.substr($code, 79, -57);
}
else{
$code = substr($code, 86, -57);
}
echo $code;
}
Note that this only works for PHP 5. If you are using PHP 4, you will have to change the numbers 79 and -57 as highlight_string() uses the tag <font> (instead of <span>) prior to PHP 5.
Examples of usage:
// it's hard to display the output... try it yourself! ;)
CodeHigh('$test = "string";');
CodeHigh('print_r($_SERVER); phpinfo(); ');
CodeHigh('$test = "string";');
CodeHigh('print_r($_SERVER); phpinfo(); ');
File extension
Detects the file extension from a given string. (Same function as filetype(), except that the supplied argument does not have to be a file but only a string.) This function basically returns the text preceeding the last dot (.).function Filext($str){
$str = (string)$str;
if(strpos($str, '.') === false){ return false; }
$ext = preg_replace('/^.*\.(.*?)$/', '\1', $str);
if($ext == ''){
return false;
}
return $ext;
}
$str = (string)$str;
if(strpos($str, '.') === false){ return false; }
$ext = preg_replace('/^.*\.(.*?)$/', '\1', $str);
if($ext == ''){
return false;
}
return $ext;
}
Note that you should use strtolower() for $ext if you want this function to be case-insensitive.
Examples:
echo filext('test.txt'); // returns 'txt'
$file = 'filename.HTmL';
echo filext($file); // returns 'HTmL'
$var = filext('TES12asd'); // returns false
$file = 'filename.HTmL';
echo filext($file); // returns 'HTmL'
$var = filext('TES12asd'); // returns false
Anonymize IPs
AnonIP() is a function that allows you to show parts of an IP address in a partially anonymized form. It will output an IP address like 178.162.87.50 as 178.162.76.xx.function AnonIP($ip){
$last = preg_replace('/.*\.([0-9]{1,3})$/', '\\1', $ip);
$ip = preg_replace('/(.*\.)([0-9]{1,3})$/', '\\1', $ip);
$ip .= str_repeat('x', strlen($last));
return $ip;
}
$last = preg_replace('/.*\.([0-9]{1,3})$/', '\\1', $ip);
$ip = preg_replace('/(.*\.)([0-9]{1,3})$/', '\\1', $ip);
$ip .= str_repeat('x', strlen($last));
return $ip;
}
Note: Unexpected results are possible if you don't supply a valid IP address. If you aren't sure, try validating your input first. Something like this:
if(preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $var)){
// $var is a correct IP
}
// $var is a correct IP
}
Examples of usage:
echo AnonIP('124.65.183.320'); // outputs 124.65.183.xxx

