检查单词是否在一组单词中

这个问题在这里已经有了答案:

  • 如何检查一个字符串是否包含特定单词? 37个答案

  • <?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


    你可以结合in_array和explode函数

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

    或者如果你想要一个更可读的版本

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

    上一篇: Check if word is inside a group of words

    下一篇: How to find if a variable contains certain character or not ? PHP