Q:

how to find word in sentence, use string or another at php

find word position in sentence, example : Sentence : i like manggo juice and apple juice. find word : juice  and the answer is the word "juice" found at position 4 and 7
before, i used strpos, but the answer is -> the word "juice" found at 14 and 30. i wanna like this -> the word "juice" found at 4 and 7. please help.. really really help me.. because, i'm newbie in php.

A:

Just use explode function to split your input according to the spaces. Loop through the array and then print the index number + 1 where the string juice is found.
$string = "i like manggo juice and apple juice";
$parts = explode(" ", $string);
foreach ($parts as $key => $value) {
    if ($value === "juice")
    {
        echo ($key + 1)."\n";
    }
}

