From 44e65f9d2d95dfe926811171da15e537053c64b5 Mon Sep 17 00:00:00 2001 From: Ronan Guilloux Date: Sun, 8 May 2011 18:08:13 +0200 Subject: [PATCH] Adding new helpers --- CHANGELOG | 11 --------- README.md | 3 ++- lib/class.http.php | 3 +++ lib/class.space.php | 54 +++++++++++++++++++++++++++++++++++++++---- lib/class.strings.php | 40 +++++++++++++++++++++++++++++++- lib/class.web2.php | 15 ++++++++++++ 6 files changed, 109 insertions(+), 17 deletions(-) delete mode 100644 CHANGELOG diff --git a/CHANGELOG b/CHANGELOG deleted file mode 100644 index d7607c5..0000000 --- a/CHANGELOG +++ /dev/null @@ -1,11 +0,0 @@ -commit 8c3061c83d6ac77b9d56f42d95f5f43d72adff83 -Author: Ronan Guilloux -Date: Fri Apr 15 08:34:30 2011 +0200 - - Adding Strings utility class - -commit cfd0a3c4014c4a9530e5b3e764fab180f82da54b -Author: Ronan Guilloux -Date: Thu Apr 7 14:11:42 2011 +0200 - - first commit diff --git a/README.md b/README.md index 1db3b07..97ccc01 100644 --- a/README.md +++ b/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/ diff --git a/lib/class.http.php b/lib/class.http.php index 27335dc..fb4c326 100644 --- a/lib/class.http.php +++ b/lib/class.http.php @@ -42,6 +42,9 @@ public static function GetRealIpAddr() } /** + * Force Download + * + * @author Alessio Delmonti * @param $file - path to file */ public static function ForceDownload($file) diff --git a/lib/class.space.php b/lib/class.space.php index 67efc77..cd9df51 100644 --- a/lib/class.space.php +++ b/lib/class.space.php @@ -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; + } diff --git a/lib/class.strings.php b/lib/class.strings.php index 09708c9..8c34fb6 100644 --- a/lib/class.strings.php +++ b/lib/class.strings.php @@ -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), @@ -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; + } } diff --git a/lib/class.web2.php b/lib/class.web2.php index a418647..60d7753 100644 --- a/lib/class.web2.php +++ b/lib/class.web2.php @@ -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 *