Last active
October 27, 2018 00:14
-
-
Save timneutkens/c5ff3b9ddff9fbd545654c32ab6a4a57 to your computer and use it in GitHub Desktop.
Get excerpt of string
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 | |
/** | |
* 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