如何在php中找到包含另一个字符串子串的字符串作为子串?

How to find the string that contains substring of another string as a substring in php?

$a = "The quick brown fox jumps over a lazy dog.";
$b = array("The lazy dog sleeps under the tree.", "...", ...);

如何在字符串 $a? 中找到包含 "lazy dog" 的 $b[0] 问题是,我事先不知道搜索到的子字符串是 "lazy dog",有些东西像数组的 array_intersect。

来自 github 的

This Gist 可能需要稍作调整,但应该能满足您的需求:

function longest_common_substring($words) {
    $words = array_map('strtolower', array_map('trim', $words));
    $sort_by_strlen = create_function('$a, $b', 'if (strlen($a) == strlen($b)) { return strcmp($a, $b); } return (strlen($a) < strlen($b)) ? -1 : 1;');
    usort($words, $sort_by_strlen);
    $longest_common_substring = array();
    $shortest_string = str_split(array_shift($words));
    while (sizeof($shortest_string)) {
        array_unshift($longest_common_substring, '');
        foreach ($shortest_string as $ci => $char) {
            foreach ($words as $wi => $word) {
                if (!strstr($word, $longest_common_substring[0] . $char)) {
                    break 2;
                }
            }
            $longest_common_substring[0] .= $char;
        }
        array_shift($shortest_string);
    }

    usort($longest_common_substring, $sort_by_strlen);
    return trim(array_pop($longest_common_substring));
}

// usage:
echo longest_common_substring(array('The quick brown fox jumped over the lazy dog', 'I jumped over the lazy bear'));