So basically I hard an extremely large string, and I would just like to save the first 4 words of it.
I ALMOST had this working, although there are some cases that break it.
Here is my current code:
$title = "blah blah blah, long paragraph goes here";
//Make title only have first 4 words
$pieces = explode(" ", $title);
$first_part = implode(" ", array_splice($pieces, 0, 4));
$title = $first_part;
//title now has first 4 words
The main cases that break it are line-breaks
. If I have a paragraph like this:
Testing one two three
Testing2 a little more three two one
The $title
would be equal to Testing one two three Testing2
Another example:
Testing
test1
test2
test3
test4
test5
test6
sdfgasfgasfg fdgadfgafg fg
Title would equal = Testing test1 test2 test3 test4 test5 test6 sdfgasfgasfg fdgadfgafg fg
For some reason it is grabbing the first word on the next line aswel.
Does anyone have any suggestions to how to fix this?