Php 确定数组中的哪个字在字符串中
我有一个字符串:Php 确定数组中的哪个字在字符串中,php,Php,我有一个字符串: $str = "Hello is a greeting"; 我有一系列的词: $equals = array("is", "are", "was", "were", "will", "has", "have", "do", "does"); 我正在尝试查看字符串中的单词: $words = explode(" ", $str); $new_association = false; foreach($words as $word) { if(in_array($wor
$str = "Hello is a greeting";
我有一系列的词:
$equals = array("is", "are", "was", "were", "will", "has", "have", "do", "does");
我正在尝试查看字符串中的单词:
$words = explode(" ", $str);
$new_association = false;
foreach($words as $word) {
if(in_array($word, $equals)) {
$new_association = true;
$e['response'] = 'You made an association.';
// determine which 'equals' word was used.
// $equal_used = 'is';
}
}
如何确定使用了哪一个相等词?上面马克的回答是比较好的,但如果你想坚持目前的方法:
$new_asscociation = false;
$equal_used = array_intersect($equals, explode(' ', $str));
if (!empty($equal_used)) {
$new_asscociation = true;
var_dump($equal_used);
}
$words = explode(" ", $str);
$new_association = false;
foreach($words as $word) {
if(in_array($word, $equals)) {
$new_association = true;
$e['response'] = 'You made an association.';
// determine which 'equals' word was used.
$equal_used = $word;
}
}
如果我只想要以字符串形式出现的值怎么办?如果有多个值,您希望哪一个作为字符串。但是您可以执行
$equal\u used=内爆(“”,$equal\u used)代码>以创建字符串