Snippet
- ////////////////////////////////////////////////////////////////////////////////////////////
- // USAGE
- //
- // [if second argument is false the word length is not truncated
- // [if second argument is numeric that it the max length of the returned word
- // examples:
- // $word_string = clean_spaces(' string of words with different number of spaces ', false);
- // $word_string = clean_spaces(' string of words with different number of spaces ', 3);
- //
- function clean_spaces($words_in, $length)
- {
- if( $length ){
- $words_check = preg_replace('/\s\s+/', '', trim($words_in));
- $words_len = explode('', $words_check );
- foreach( $words_lenas$word => $value){
- if( strlen( $value )> $length ){
- // un comment this line to show the dots of truncated words
- //$word_fixed = substr( $value, 0, $length) . '…';
- $word_fixed = substr( $value, 0, $length);
- $words_out = $words_out . '' . $word_fixed;
- }
- }
- }else{
- $words_out = preg_replace('/\s\s+/', '', trim($words_in));
- }
- return$words_out;
- }
<!--break-->