Last active
February 28, 2022 11:35
-
-
Save damianwajer/bc7bacdd185b1d173234 to your computer and use it in GitHub Desktop.
[PHP] Limit text length by characters
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 | |
/** | |
* 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 ) ) ? '…' : ''; | |
$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