-
-
Save maaddae/b34a2726f95939d0ade40fdac1442e95 to your computer and use it in GitHub Desktop.
Get excerpt of string
This file contains hidden or 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 | |
/** | |
* Small utility function to get an excerpt. Standard length is 100 characters | |
* | |
* @param $string | |
* @param int $start_postion | |
* @param int $max_length | |
* | |
* @return string | |
*/ | |
function get_excerpt( $string, $start_postion = 0, $max_length = 100 ) { | |
if ( strlen( $string ) <= $max_length ) { | |
return $string; | |
} | |
$excerpt = substr( $string, $start_postion, $max_length - 3 ); | |
$last_space = strrpos( $excerpt, ' ' ); | |
$excerpt = substr( $excerpt, 0, $last_space ); | |
$excerpt .= '...'; | |
return $excerpt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment