Limit string to first 5 words or first 42 characters in PHP

If I have a string in PHP, which is a shockingly long string in PHP, and I want to shorten it, and then add something to it.

I want to shorten it to the first 6 words or 42 characters, no matter what is shorter, and then add “...” to it if it has been shortened.

The only case when it will not be abbreviated, but "..." will not be added if it was originally less than 6 words or 42 characters.

How can I do this in PHP?

Logically, I would think that I would divide the string into empty space, and then add each thing in front of white space to the array and select only the first 6 elements from this array and write them in a new line.

Here is the code that I still have:

str_1 = 'The quick brown fox jumped over the lazy dog'; $words = explode(" ", $str_1); $counter = 0; str_2 = ''; foreach($words as $word){ if($counter < 5){ //append $words[counter] to str_2; counter++; } else{ break; } } 

I do not know how to make a remainder for the number of characters, comparison or addition.

Does anyone have any ideas?

+6
source share
4 answers

This function I made looks pretty neat:

 function truncate($input, $maxWords, $maxChars) { $words = preg_split('/\s+/', $input); $words = array_slice($words, 0, $maxWords); $words = array_reverse($words); $chars = 0; $truncated = array(); while(count($words) > 0) { $fragment = trim(array_pop($words)); $chars += strlen($fragment); if($chars > $maxChars) break; $truncated[] = $fragment; } $result = implode($truncated, ' '); if ($input == $result) { return $input; } else { return preg_replace('/[^\w]$/', '', $result) . '...'; } } 

Some tests:

 $str = 'The quick brown fox jumped over the lazy dog'; echo truncate($str, 5, 42); // The quick brown fox jumped... echo truncate($str, 3, 42); // The quick brown... echo truncate($str, 50, 30); // The quick brown fox jumped over the... echo truncate($str, 50, 100); // The quick brown fox jumped over the lazy dog 

He will not cut the words in half, so if the word pushes the character counter to the limit provided, it will be ignored.

+10
source

count words (implies spaces in spaces):

 $words = explode(' ', $string); $wordCount = count($words ); 

character count

 $length = strlen($string); 

together

 if($wordCount > 5) { $words = array_slice($words, 0, 5); $string = implode(' ', $words); $length = strlen($string); } if($length > 42) { $string = substr($string , 0, 42); } 
+3
source

This will give you the first 6 words or the first n words where the total word length is less than 42.

 $words = explode(' ', $string); $newstring = ""; $i=0; while (strlen($newstring . $words[$i]) <= 42) && $i < 5) { $newstring .= $words[$i]; } if (strlen($string) > strlen($newstring)) $newstring .= "..."; 
+2
source
 $string = "Your Text Here"; $string_length = 80; // give a random character value including whitespace. if(strlen($string)>$string_length){ do{ $new_string = substr($string,0,$string_length); $string_length++; }while(ctype_graph(substr($new_string,-1))); } else { $new_string = $string; } echo $new_string; 
0
source

Source: https://habr.com/ru/post/945739/


All Articles