Created
August 10, 2018 10:59
-
-
Save greatsami/39bed74be1fd566e84dd3fd4ad22e678 to your computer and use it in GitHub Desktop.
Print limited words from paragraph
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 | |
function word_limiter($str, $limit = 100, $end_char = '…'){ | |
$str2 = strip_tags($str); | |
//$str2 = $str; | |
if (trim($str2) == ''){ | |
return $str2; | |
} | |
preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str2, $matches); | |
if (strlen($str2) == strlen($matches[0])){ | |
$end_char = ''; | |
} | |
return rtrim($matches[0]).$end_char; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment