如何搜索字符串中的关键字是否存在于数组中?
问题描述:
我有一个最多200个字符的字符串,也是4000个元素的数组。数组中的每个元素都不相同,最多可以包含3个单词。如何搜索字符串中的关键字是否存在于数组中?
我想获得所有这些关键字,我在我的字符串中存在的数组中。
对于例如为:
$海峡=“这是我的测试字符串请复制此字符串。”;
$ arr = ['is','my test','test string','strings'];
所以,我应该从数组中找到结果,我的测试,测试字符串,字符串关键字。
答
你可以做这样的事情:
<?php
$str = "This is my test string. Please copy this strings.";
$arr = ['is', 'my test', 'test string', 'strings'];
$results = [];
foreach($arr as $stringToEvaluate){
// If the $stringToEvaluate is found in $str
// then you push it to the $results array
if(strpos($str, $stringToEvaluate) !== false){
$results[] = $stringToEvaluate;
}
}
print_r($results);
+0
谢谢先生。你帮了很多忙。 :) –
+0
我很高兴! – nanocv
@Ohgidwhy:没有,在链接已共享,这家伙知道他wabts寻找的子字符串。就我而言,我不知道什么是要搜索的子字符串。你可以参考我的例如谢谢。 –