Skip to content

Commit

Permalink
Adding new helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanguilloux committed May 8, 2011
1 parent d2e1cdf commit 44e65f9
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 17 deletions.
11 changes: 0 additions & 11 deletions CHANGELOG

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
@@ -1 +1,2 @@
Simple PHP lib, tools & helpers, all GNU GPL'd.
Simple PHP lib, tools & helpers, all GNU GPL'd.
See also http://www.jonasjohn.de/snippets/php/
3 changes: 3 additions & 0 deletions lib/class.http.php
Expand Up @@ -42,6 +42,9 @@ public static function GetRealIpAddr()
}

/**
* Force Download
*
* @author Alessio Delmonti
* @param $file - path to file
*/
public static function ForceDownload($file)
Expand Down
54 changes: 50 additions & 4 deletions lib/class.space.php
Expand Up @@ -40,10 +40,56 @@ public static function Distance($latA, $lonA, $latB, $lonB)

// do trigonometry magic
$d =
sin($dLat/2) * sin($dLat/2) +
cos($latA) * cos($latB) * sin($dLon/2) *sin($dLon/2);
sin($dLat/2) * sin($dLat/2) +
cos($latA) * cos($latB) * sin($dLon/2) *sin($dLon/2);
$d = 2 * asin(sqrt($d));
return $d * 6371;
}

}

/**
* Calculate the distance from a point A to a point B, using latitudes and longitudes.
* The function can return the distance in miles, kilometers, or nautical miles
*
* @author zipcodeworld
* @example echo distance(32.9697, -96.80322, 29.46786, -98.53506, "k")." kilometers";
*/
public static function KmDistance($lat1, $lon1, $lat2, $lon2, $unit = 'k')
{

$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);

if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}

/**
* Geocoding, from address to coordinates lat long
*
* @author snipplr.com
* @param string $address, postal address
* @return array coords
*/
public static function getLatLong($address)
{
if (!is_string($address))die("All Addresses must be passed as a string");
$_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
$_result = false;
if($_result = file_get_contents($_url))
{
if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
$_coords['lat'] = $_match[1];
$_coords['long'] = $_match[2];
}
return $_coords;
}
40 changes: 39 additions & 1 deletion lib/class.strings.php
Expand Up @@ -86,7 +86,7 @@ public static function Lcwords($string)
* @return unsmartquoted
* @see http://shiflett.org/blog/2005/oct/convert-smart-quotes-with-php
*/
protected function convert_smart_quotes($string)
public static function ConvertSmartQuotes($string)
{
$search = array(chr(145),
chr(146),
Expand All @@ -102,4 +102,42 @@ protected function convert_smart_quotes($string)

return str_replace($search, $replace, $string);
}

/**
* Get the text between $start and $end
*
* @author jonasjohn.de
* @param string $content (all content)
* @param string $start
* @param string $end
* @return string
*/
public static function GetBetween($content, $start, $end)
{
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}


/**
* Return (bool) if $x contains $y
*
* @author jonasjohn.de
* @param string $str
* @param string $content
* @param bool $ignorecase
* @return boolean
*/
public static function Contains($str, $content, $ignorecase = true)
{
if ($ignorecase){
$str = strtolower($str);
$content = strtolower($content);
}
return strpos($content,$str) ? true : false;
}
}
15 changes: 15 additions & 0 deletions lib/class.web2.php
Expand Up @@ -34,6 +34,21 @@ public static function ShowGravatar($email, $size, $default, $rating)
height="'.$size.'px" />';
}

/**
* Get website favicon from url
*
* @author snipplr.com
* @param string $url
* @return string favicon url
*/
public static function GetFavicon($url)
{
$url = str_replace("http://",'',$url);
//TODO : regex that removes the last part of the url and detects errors
return "http://www.google.com/s2/favicons?domain=".$url;



/**
* Takes a string and makes it SEO and URL friendly
*
Expand Down

0 comments on commit 44e65f9

Please sign in to comment.