Skip to content

Instantly share code, notes, and snippets.

@timneutkens
Last active October 27, 2018 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timneutkens/c5ff3b9ddff9fbd545654c32ab6a4a57 to your computer and use it in GitHub Desktop.
Save timneutkens/c5ff3b9ddff9fbd545654c32ab6a4a57 to your computer and use it in GitHub Desktop.
Get excerpt of string
<?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