Skip to content

Instantly share code, notes, and snippets.

@damianwajer
Last active February 28, 2022 11:35
Show Gist options
  • Save damianwajer/bc7bacdd185b1d173234 to your computer and use it in GitHub Desktop.
Save damianwajer/bc7bacdd185b1d173234 to your computer and use it in GitHub Desktop.
[PHP] Limit text length by characters
<?php
/**
* limit_text_length
* Function to limit text length by characters.
*
* @param string $string
* @param int $length
*
* @return string
*/
function limit_text_length( $string, $length = 0 ) {
$string = html_entity_decode( $string, ENT_QUOTES, 'utf-8' );
$sufix = ( $length < strlen( $string ) ) ? '&hellip;' : '';
$string = mb_substr( $string, 0, $length, 'utf-8' );
return trim( $string ) . $sufix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment