Created
February 16, 2012 20:50
-
-
Save slowmove/1847745 to your computer and use it in GitHub Desktop.
A simple class with some static helpfunctions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class HelpFunctions | |
{ | |
/** | |
* Get the relative time between now and a given timestamp | |
* @param string $timestamp | |
* @return string | |
*/ | |
static function relativeTime($timestamp) | |
{ | |
$diff = time() - strtotime($date); | |
if ($diff<60) | |
return ('mindre än en minut sedan'); | |
else if ($diff < 120) | |
return ('en minut sedan'); | |
else if ($diff < (60*60)) | |
return (round($diff/60).' minuter sedan'); | |
else if ($diff < (120*60)) | |
return ('ungefär en timme sedan'); | |
else if ($diff < (24*60*60)) | |
return ('ungefär '.round($diff/3600).' timmar sedan'); | |
else if ($diff < (48*60*60)) | |
return ('1 dag sedan'); | |
else | |
return (round($diff/86400).' dagar sedan'); | |
} | |
/** | |
* Get the IP address of the visitor | |
* @return string | |
*/ | |
static function getIp() | |
{ | |
if (!emptyempty($_SERVER['HTTP_CLIENT_IP'])) | |
{ | |
$ip=$_SERVER['HTTP_CLIENT_IP']; | |
} | |
elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR'])) | |
//to check ip is pass from proxy | |
{ | |
$ip=$_SERVER['HTTP_X_FORWARDED_FOR']; | |
} | |
else | |
{ | |
$ip=$_SERVER['REMOTE_ADDR']; | |
} | |
return $ip; | |
} | |
/** | |
* Creates an excerpt, perfect for teasers etc. | |
* @param string $string | |
* @param int $limit | |
* @param string $break | |
* @param string $pad | |
* @return string | |
*/ | |
static function truncateString($string, $limit, $break=".", $pad="...") { | |
// return with no change if string is shorter than $limit | |
if(strlen($string) <= $limit) | |
return $string; | |
// is $break present between $limit and the end of the string? | |
if(false !== ($breakpoint = strpos($string, $break, $limit))) { | |
if($breakpoint < strlen($string) - 1) { | |
$string = substr($string, 0, $breakpoint) . $pad; | |
} | |
} | |
return $string; | |
} | |
/** | |
* Convert URLs within strings into hyperlinks | |
* @param string $text | |
* @return string $text | |
*/ | |
static function makeClickableLinks($text) { | |
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)', | |
'<a href="\1">\1</a>', $text); | |
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)', | |
'\1<a href="http://\2">\2</a>', $text); | |
$text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})', | |
'<a href="mailto:\1">\1</a>', $text); | |
return $text; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment