I need to know how to get the first n words from text stored in my DB in PHP?
for example if there is some text in my DB like this one :
"word1 word2 word3 word4 text one test four five"
How I can get the first 4 or five words from this text?
I need to know how to get the first n words from text stored in my DB in PHP?
for example if there is some text in my DB like this one :
"word1 word2 word3 word4 text one test four five"
How I can get the first 4 or five words from this text?
You can use the explode
function to split the string by space and get each word:
$words = 'word1 word2 word3 word4 text one test four five';
$words_array = explode(' ', $words);
And then you can use the array_chunk
function to get number of words:
print_r(array_chunk($words_array, 4, true));