繁体   English   中英

如何在PHP中返回包含特定单词的句子

[英]How to return sentence that contains specific words in PHP

就像标题一样,我想知道只有在其中包含特定单词的情况下,我才能轻松地返回句子吗?

可以说我有一个数组:

 $strings = array("Hello World", "Hello Earth", "Hi World"); $match = "Hello"; foreach ($strings as $string) { echo $string; //And here i want to return only strings with "Hello", //In this case it should only return: //Hello World, //Hello Earth } 

有人可以帮忙吗? :)

使用strpos()检查字符串是否包含所选单词。

$strings = array("Hello World", "Hello Earth", "Hi World");
$match = "Hello";
foreach ($strings as $string) {
  if (strpos($string, $match) !== false) {
      echo $string;
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM