Check if word is inside a group of words

This question already has an answer here:

  • How do I check if a string contains a specific word? 37 answers

  • <?php
     $os = array("Mac", "NT", "Irix", "Linux");
     if (in_array("Irix", $os)) {
    echo "Got Irix";
      }
      if (in_array("mac", $os)) {
       echo "Got mac";
      }
      ?>
    

    http://php.net/manual/en/function.in-array.php


    You can combine the in_array and explode functions

    echo ((in_array($searchTerm, explode(",", $cities)))?"Yes":"No");
    

    or if you want a more readable version

    $resultArray = explode(",", $cities);
    $result = (in_array($searchTerm, $resultArray);
    if ($result) {
       echo "Yes"
    } else {
       echo "No";
    }
    
    链接地址: http://www.djcxy.com/p/13126.html

    上一篇: 检查字符串是否包含单词(不是一个句子字符串)

    下一篇: 检查单词是否在一组单词中