如何搜索字符串中的关键字是否存在于数组中


How to search if keywords from a string are present in an array?

我有一个最多200个字符的字符串,一个4000个元素的数组。数组中的每个元素都是不同的,最多可以包含3个单词。

我想要得到字符串中所有的关键字这些关键字存在于数组中

例如

:

$str = "这是我的测试字符串。

$ arr =[‘是’,我的测试,测试字符串,'字符串'],

所以,我应该得到结果,我的测试,测试字符串,字符串关键字从数组中找到。

你可以这样做:

<?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);