Php 搜索字符串上的多维数组以检查相关单词是否存在

Php 搜索字符串上的多维数组以检查相关单词是否存在,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我想在文本中创建一个动词搜索器 $conjugation=array(); $conjugation[0]=array("sein","bin","bist","ist","war","gewesen"); $conjugation[1]=array("haben","habe","hast","hat","hatte","gehabt"); $conjugation[2]=array("lesen","lese","liest","liest","las","gelesen"); $conju

我想在文本中创建一个动词搜索器

$conjugation=array();
$conjugation[0]=array("sein","bin","bist","ist","war","gewesen");
$conjugation[1]=array("haben","habe","hast","hat","hatte","gehabt");
$conjugation[2]=array("lesen","lese","liest","liest","las","gelesen");
$conjugation[3]=array("gehen","gehe","geht","gehts","ging","gegangen");
我想搜索文本中的每个动词变位,并用替换的变位动词检索它

  $string="Ich habe das Buch gelesen. Du hast einen apfel. Sie ist nicht gegangen";

  ...<span title='gehen'>gegangen</span>...
我怎样才能得到预期的结果?

预期产量

 Ich <span title='haben'>habe</span> das Buch 
 <span title='lesen'>gelesen</span>. 
 Du <span title='haben'>hast</span> einen apfel. 
 Sie <span title='sein'>ist</span> nicht <span title='gehen'>gegangen</span>
<代码>我有一个 盖勒森。 你有阿普费尔。 你是尼希特·格甘根吗
创建一个关联数组,将共轭映射到动词。然后用它来创建替换

$verb_map = array();
foreach ($verb as $i => $v) {
    foreach ($conjugation[$i] as $w) {
        $verb_map[$w] = $v;
    }
}
$words = explode(' ', $string);
$new_words = array();
foreach ($words as $w) {
    $lower_word = preg_replace('/[^a-z]/', '', strtolower($w));
    if (isset($verb_map[$lower_word])) {
        $new_words[] = "<span title='{$verb_map[$lower_word]}'>$w</span>";
    } else {
        $new_words[] = $w;
    }
}
$new_string = implode(' ', $new_words);
echo $new_string;
您可以使用查找这些单词中的任何一个,并用相应的
替换它们:

foreach($verbConjugations as$verbConjugations){
$pattern='/\b(?.introde('|',$verb共轭)。'\b/i';
$string=preg\u replace\u回调($pattern,function($matches)use($verbConjugations){
返回“{$matches['word']}”;
},$string);
}
echo$字符串;

演示:

当你在数组中调用
时,你在增加
$x
,所以你在回显下一个单词。你能显示预期的输出是什么吗?
$verb
与此有什么关系?我已经更新并发布了预期的结果。你为什么只搜索
$conjugation[0]
habe
位于
$conjugation[1]
中。我正在测试,但它无法检索带标题的单词。在搜索之前,您需要将单词转换为小写。我已经添加了。
[…]
只是变成了它无法识别的
数组(…)
gelesen
因为它有一个点(.)。我添加了
preg_replace
来删除单词中的所有非字母。哇,这似乎是一个很好的代码。但由于某些原因,它在我的笔记本电脑服务器中不起作用。我想我有一个旧的php版本…t将是重写它以与旧php匹配的一种方法?此外,现在删除了数组
$verb
,并将其添加到每个子数组的第一个字段中的主动词。(我编辑了帖子)@max更新了答案并切换回了旧的数组样式。如果它仍然不起作用,这意味着你正在运行PHP5.3或更低版本,这太旧了(不建议使用PHP5本身,尤其是它的旧版本)。它只是我目前用来学习语言的自己的服务器…所以…我认为你的代码在这里不起作用。有没有办法使它适合这个版本?我认为问题在于这个函数
preg\u replace\u callback()
@max Nice。但是,如果你正在学习,你应该强烈考虑升级你的PHP版本之一。祝你好运
$verb_map = array();
foreach ($verb as $i => $v) {
    foreach ($conjugation[$i] as $w) {
        $verb_map[$w] = $v;
    }
}
$words = explode(' ', $string);
$new_words = array();
foreach ($words as $w) {
    $lower_word = preg_replace('/[^a-z]/', '', strtolower($w));
    if (isset($verb_map[$lower_word])) {
        $new_words[] = "<span title='{$verb_map[$lower_word]}'>$w</span>";
    } else {
        $new_words[] = $w;
    }
}
$new_string = implode(' ', $new_words);
echo $new_string;
$verbs = array(
    array("verb" => "haben", "conjugation" => array("habe","hast","hat","hatte","gehabt")),
    array("verb" => "sein", "conjugation" => array("bin","bist","ist","war","gewesen")),
    ...
);
foreach ($conjugation as $verbConjugations) {
  $pattern = '/\b(?<word>' . implode('|', $verbConjugations) . ')\b/i';
  $string = preg_replace_callback($pattern, function ($matches) use ($verbConjugations) {
    return "<span title='{$verbConjugations[0]}'>{$matches['word']}</span>";
  }, $string);
}

echo $string;