Skip to content

Instantly share code, notes, and snippets.

View shyamsalimkumar's full-sized avatar

Shyam S Kumar shyamsalimkumar

  • Berlin
  • 17:00 (UTC +02:00)
View GitHub Profile
@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: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: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: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: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: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: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;
}