获取两个或多个php字符串之间的匹配字符串

获取两个或多个php字符串之间的匹配字符串,php,string,Php,String,我有两条线,第一条是它的 $str1 = "Hi stackoverflow community, I am new user and i have question, can you help me please"; $str2 = "Hi gamers community, I am new user and i have question, any one can help me please"; 我想提取所有超过(2)个单词的句子及其 社区,我是新用户,我有问题 及 请帮帮我 所以在搜

我有两条线,第一条是它的

$str1 = "Hi stackoverflow community, I am new user and i have question, can you help me please";
$str2 = "Hi gamers community, I am new user and i have question, any one can help me please";
我想提取所有超过(2)个单词的句子及其

社区,我是新用户,我有问题

请帮帮我

所以在搜索之后,我做了一些代码,比如madearray,把它推到相似的单词和单词位置,然后比较单词的位置,但是遇到了一些问题,所以我做了更多的搜索,找到了这个方法

function getLongestMatchingSubstring($str1, $str2)
{
    $len_1 = strlen($str1);
    $longest = '';
    $a = array();
    for($i = 0; $i < $len_1; $i++){
        for($j = $len_1 - $i; $j > 0; $j--){
            $sub = substr($str1, $i, $j);
            if (strpos($str2, $sub) !== false && strlen($sub) > strlen($longest)){
                $longest = $sub;
                $a[] = $sub;
                break;
            }
        }
    }
    return $a;
}
我们可以看到,
您能帮我一下吗
结果中不存在,因为if station
strlen($sub)>strlen($longest)

我试着编辑if语句,但失败了,给了我一个可怕的结果,比如

0 => string ' community, I am new user and i have question, ' (length=47)
  1 => string 'community, I am new user and i have question, ' (length=46)
  2 => string ' I am new user and i have question, ' (length=36)
  3 => string 'I am new user and i have question, ' (length=35)
  4 => string ' am new user and i have question, ' (length=34)
  5 => string 'am new user and i have question, ' (length=33)
  6 => string ' new user and i have question, ' (length=31)
  7 => string 'new user and i have question, ' (length=30)
  8 => string ' user and i have question, ' (length=27)
  9 => string 'user and i have question, ' (length=26)
  10 => string ' and i have question, ' (length=22)
  11 => string 'and i have question, ' (length=21)
  12 => string ' i have question, ' (length=18)
  13 => string 'i have question, ' (length=17)
  14 => string ' have question, ' (length=16)
  15 => string 'have question, ' (length=15)
第二个问题需要很长时间


这工作有对数吗?或者和想法对if语句进行一些编辑

我正在阅读你的问题,因为给出了一个超过两个单词的指针。找到包含它们的句子,如果我误解了,请纠正我

<?php

$sentences =
[
    'I like my rubber ducky.',
    'Paperclips make me happy, I have lots.'
];

$needle = 'make me happy';

foreach($sentences as $sentence) {
    if(strpos($sentence, $needle) !== false) {
        echo $sentence;
    }
}

也许给出输入和想要的输出的例子。我不太明白您想要达到的目的。@Progrock在两个字符串之间查找匹配项,这两个字符串都没有
$pinder
只是两个字符串比较并查找matches@dailytube,所以给出:
空间很大,真的很大,你不会相信空间有多大,它太大了。
而字符串
真的太大了
有多大
,如果它返回的话:
,你不会相信的
?你说的火柴是什么意思?
<?php

$sentences =
[
    'I like my rubber ducky.',
    'Paperclips make me happy, I have lots.'
];

$needle = 'make me happy';

foreach($sentences as $sentence) {
    if(strpos($sentence, $needle) !== false) {
        echo $sentence;
    }
}
Paperclips make me happy, I have lots.