php - How to search for certain words in array in a sentence using PHP
问题描述
I am trying to search for certain word in a sentence. To that effect, I leverage solutions found (source), which works fine for searching just a word.
My issue is that I need to search the words in the array in a sentence something like
$findme = array("food", "water", "salt");
How can I do that?
Here is the code:
$mystring = 'my best food is Jellof Rice';
$findme = 'food';
$pos = strpos($mystring, $findme);
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'.";
}
else {
echo "The string '$findme' was found in the string '$mystring',";
echo " and exists at position $pos.";
}
解决方案
try something like this one
$mystring = 'text goes here';
$Found = false;
$findme = array("food", "water", "salt");
foreach($findme as $singleValue ){
if ($strpos($mystring, $singleValue ) != false) {
$Found = true;
}
}
推荐阅读
- ajax - Bearer 令牌如何在使用 Asp.net MVC 5 的基于令牌的身份验证中工作?
- ionic3 - 如何使用 ionic3 构建 exe 文件(桌面应用程序)
- ssas - SSAS 错误:无法加载文件或程序集“Microsoft.AnalysisServices,版本=13.0.0.0,文化=中性,PublicKeyToken=89845dcd8080cc91”
- ios - 如何在 Vodolazkyi/VACalendar swift 中选择范围?
- bash - bash (CLI) 命令如何在 Linux 内部工作?
- python - 如何使用 spyder 在单独的窗口中显示视频
- xamarin - 使用 android 模拟器通过 ZXing.Net.Mobile 进行 qr 扫描时的视频像素化
- python - Python:删除字符串中的特殊字符
- javascript - Javascript电话验证
- llvm - 如何使用llvm实现虚拟表