Remove Words step by step from Left to Right in PHP String
Get the solution ↓↓↓I have a string and I want to remove words from its left to right step by step.
$n = 'Dettol Antibacterial Surface Cleansing Wipes';
$arr = str_word_count($n, 1);
for ($x = 0; $x < count($arr); $x++) {
echo $arr[$x].'<br />';
}
What I want is to get 4 strings out of it removing words left to right.
string1= Antibacterial Surface Cleansing Wipes
string2= Surface Cleansing Wipes
string3= Cleansing Wipes
string4= Wipes
Answer
Solution:
Quick and dirty way is to useexplode
, remove first element byarray_shift
and thenimplode
.
$n = 'Dettol Antibacterial Surface Cleansing Wipes';
$words = explode(' ', $n);
while (!empty($words)) {
array_shift($words);
echo implode(' ', $words) . '<br>';
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.