Skip to content

Instantly share code, notes, and snippets.

View shyamsalimkumar's full-sized avatar

Shyam S Kumar shyamsalimkumar

  • Berlin
  • 22:32 (UTC +01:00)
View GitHub Profile
@shyamsalimkumar
shyamsalimkumar / gist:5752278
Last active December 18, 2015 08:09
PHP - Sanitize inputs, Usage - $_POST = sanitize($_POST);
function cleanInput($input) {
$search = array(
'@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
);
$output = preg_replace($search, '', $input);
return $output;
}
@shyamsalimkumar
shyamsalimkumar / gist:5752309
Last active December 18, 2015 08:09
PHP - Get distance b/w coordinates
function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {
$theta = $longitude1 - $longitude2;
$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
$meters = $kilometers * 1000;
@shyamsalimkumar
shyamsalimkumar / gist:5752327
Last active December 18, 2015 08:09
PHP - Get all tweets of a specific hashtag
function getTweets($hash_tag) {
$url = 'http://search.twitter.com/search.atom?q='.urlencode($hash_tag) ;
echo "<p>Connecting to <strong>$url</strong> ...</p>";
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec ($ch);
curl_close ($ch);
//If you want to see the response from Twitter, uncomment this next part out:
@shyamsalimkumar
shyamsalimkumar / gist:5752348
Last active December 18, 2015 08:09
PHP - Email error log
// Our custom error handler
function nettuts_error_handler($number, $message, $file, $line, $vars){
$email = "
<p>An error ($number) occurred on line
<strong>$line</strong> and in the <strong>file: $file.</strong>
<p> $message </p>";
$email .= "<pre>" . print_r($vars, 1) . "</pre>";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
@shyamsalimkumar
shyamsalimkumar / gist:5752358
Last active December 18, 2015 08:09
PHP - Automatically creates variables with the same name as the key in the POST array
$expected=array('username','age','city','street');
foreach($expected as $key){
if(!empty($_POST[$key])){
${key}=$_POST[$key];
}
else{
${key}=NULL;
}
}
@shyamsalimkumar
shyamsalimkumar / gist:5752362
Last active December 18, 2015 08:09
PHP - Download & save a remote image on your server
function downloadFromRemote($imageUrl = '', $saveFilePath) {
$image = file_get_contents($imageUrl);
return file_put_contents($saveFilePath, $image); //save the image on local server
}
@shyamsalimkumar
shyamsalimkumar / gist:5752394
Last active December 18, 2015 08:09
PHP - Create data uri’s - Base64 encoding
function data_uri($file, $mime) {
$contents=file_get_contents($file);
$base64=base64_encode($contents);
return "data:$mime;base64,$base64";
}
@shyamsalimkumar
shyamsalimkumar / gist:5752403
Last active December 18, 2015 08:09
PHP - Detect browser language
function get_client_language($availableLanguages, $default='en'){
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($langs as $value){
$choice=substr($value,0,2);
if(in_array($choice, $availableLanguages)){
return $choice;
}
}
}
@shyamsalimkumar
shyamsalimkumar / gist:5752412
Last active December 18, 2015 08:09
PHP - Add (th, st, nd, rd, th) to the end of a number
function ordinal($cdnl){
$test_c = abs($cdnl) % 10;
$ext = ((abs($cdnl) %100 < 21 && abs($cdnl) %100 > 4) ? 'th'
: (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1)
? 'th' : 'st' : 'nd' : 'rd' : 'th'));
return $cdnl.$ext;
}
@shyamsalimkumar
shyamsalimkumar / gist:5752455
Last active December 18, 2015 08:09
PHP - Detect location by IP
function detect_city($ip) {
$default = 'UNKNOWN';
if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
$ip = '8.8.8.8';
$curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
$url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);