From: Andrey Kutejko Date: Fri, 17 May 2013 07:10:25 +0000 (+0300) Subject: detect static methods in IPF::loadFunction X-Git-Tag: 0.5~272 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=8502c8e8656706020b290303a73cff422f2a3e84;p=ipf.git detect static methods in IPF::loadFunction --- diff --git a/ipf.php b/ipf.php index 6dde361..68a1270 100644 --- a/ipf.php +++ b/ipf.php @@ -139,6 +139,10 @@ final class IPF { if (function_exists($function)) return; + if (preg_match('/^(\w+)::\w+$/', $function, $m)) { + IPF_Autoload($m[1]); + return; + } $elts = explode('_', $function); array_pop($elts); $file = strtolower(implode(DIRECTORY_SEPARATOR, $elts)).'.php'; diff --git a/ipf/utils.php b/ipf/utils.php index bda6deb..30649a6 100644 --- a/ipf/utils.php +++ b/ipf/utils.php @@ -281,5 +281,56 @@ class IPF_Utils return strtolower(preg_replace('/[^A-Z^a-z^0-9^\/\_]+/', '-', $slug)); return $slug; } + + /** + * Word Limiter + * + * Limits a string to X number of words. + * + * @param string + * @param integer + * @param string the end character. Usually an ellipsis + * @return string + */ + public static function limitWords($str, $limit=100, $end_char='…') + { + if (trim($str) == '') + return $str; + preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches); + if (strlen($str) == strlen($matches[0])) + $end_char = ''; + return rtrim($matches[0]).$end_char; + } + + /** + * Character Limiter + * + * Limits the string based on the character count. Preserves complete words + * so the character count may not be exactly as specified. + * + * @param string + * @param integer + * @param string the end character. Usually an ellipsis + * @return string + */ + function limitCharacters($str, $n=500, $end_char='…') + { + if (strlen($str) < $n) + return $str; + + $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); + + if (strlen($str) <= $n) + return $str; + + $out = ""; + foreach (explode(' ', trim($str)) as $val) { + $out .= $val.' '; + if (strlen($out) >= $n) { + $out = trim($out); + return (strlen($out) == strlen($str)) ? $out : $out.$end_char; + } + } + } }