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?
source share