Created
May 5, 2015 01:39
-
-
Save GoldenEra/a553ad81ed60d60130ab to your computer and use it in GitHub Desktop.
php:get description
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
/** | |
* [get_desc get a simple description of a long content] | |
* @param [str] $str | |
* @param integer $len [the length you want to cut,default 50] | |
* @return [string] [Result after cutting] | |
*/ | |
function get_desc($str,$len = 50){ | |
$str = strip_tags($str); | |
if (strlen($str) < $len) { | |
return $str; | |
} | |
$str = mb_substr(trim($str),0,$len - 3,'UTF-8'); | |
$num = strrpos($str, ' '); | |
$str = rtrim(substr($str,0,$num)); | |
return preg_replace("/[\W]{1,4}$/","",$str)."..."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment