如何使用php搜索字符串中的smiler单词
我有一个像这样的数组如何使用php搜索字符串中的smiler单词,php,arrays,string,Php,Arrays,String,我有一个像这样的数组$questions=['how ara you','what's products','tell me about your']和一个像这样的变量$str='hello how are you' 如果这个变量在那个数组中可用,那么我显示一个结果 $match = array_search(strtolower($str), $questions); //here that string match exaclty that time only result is co
$questions=['how ara you','what's products','tell me about your']
和一个像这样的变量$str='hello how are you'
如果这个变量在那个数组中可用,那么我显示一个结果
$match = array_search(strtolower($str), $questions); //here that string match exaclty that time only result is comming like
'how ara you'
是$questions数组的值,但是是字符串($str)'hello how are you'
,所以这里hello不在数组中,因为我没有给出结果
如果string$str='你好'
我正在获取结果
如何使用php进行单词搜索。您可以使用
stripos
搜索字符串中的字符串部分。因此,您的常用字符串位于questions
数组中,搜索字符串位于str
变量中。stripos
将搜索字符串中每个元素中出现的str
问题数组
用于以不区分大小写的方式匹配第一次出现的问题。因此,不需要将搜索字符串的大小写降低
$questions = ['how are you','what are the products','tell me about yourself'];
$str = 'hello how are you';
foreach ($questions as $item) {
if (( stripos( $item, $str ) !== FALSE ) || ( stripos( $str, $item ) !== FALSE )){
echo $item;
}
}
您还可以使用array\u filter
获取数组中的匹配项。用这个来替换这个循环
$matched = array_filter( $questions, function ( $item ) use ( $str )
{
if (( stripos( $item, $str ) !== FALSE ) || ( stripos( $str, $item ) !== FALSE )){
return $item;
}
});
@Jeto抱歉,我没有得到哪个括号?@AI Amin谢谢你的回答,但是如果我用一个字符串给出关于你自己的
,,而这个字符串没有得到回应,那么我应该给出关于你自己的,,有没有办法找到mactch分词。好的,我已经更新了答案,从两边匹配你的字符串,现在检查