I have a string as given below:
$str = "In order to successfully build your backlinks in a highly competitive industry (or if you're targeting highly competitive keywords), you have to get smart about your strategy. That means using the best back-link building tools available";
Now I want to split the string after every third word. That is I want..
split1 = in order to
split2 = successfully build your
split4 = backlinks in a
and so on till the end of the string.
I have already done it using preg_match_all but it isn't giving me what I want. So can someone help me out with it to split the string using the split() or preg_split or the explode() function.
Thank you
$split = explode(' ', $str); // Split up the whole string
$chunks = array_chunk($split, 3); // Make groups of 3 words
$result = array_map(function($chunk) { return implode(' ', $chunk); }, $chunks); // Put each group back together
$result
is:
Array
(
[0] => In order to
[1] => successfully build your
[2] => backlinks in a
[3] => highly competitive industry
[4] => (or if you're
[5] => targeting highly competitive
[6] => keywords), you have
[7] => to get smart
[8] => about your strategy.
[9] => That means using
[10] => the best back-link
[11] => building tools available
)