[Solved] Find the Position of a Word in a String? [duplicate]


you could do:

function find_word_pos($string, $word) {
    //case in-sensitive
    $string = strtolower($string); //make the string lowercase
    $word = strtolower($word);//make the search string lowercase
    $exp = explode(" ", $string);
    if (in_array($word, $exp)) { 
        return array_search($word, $exp) + 1;
    }
    return -1; //return -1 if not found
}
$str = "This is a string";
echo find_word_pos($str, "string");

4

solved Find the Position of a Word in a String? [duplicate]