根据字符串将句子中的单词提取到数组中

Extract words from sentence into array based on a string

我正在寻找最有效的解决方案,使我能够构建一个字符串模板,以便从使用模板结构的句子中提取单词和短语。

假设我有一个 string 如下:

$template = '%% is %% because %%.';

现在,假设我有一个字符串数组:

$strings = [
    'Cheese is the best thing because it is great on chips.',
    'My brother is my best friend because he\'s always been there.', 
    'Listen! Whosebug is how I am still employed because I am not afraid to ask for help.',
    'Derp... Why is it that I can\'t do this easily? Maybe it is because I need more practice.'
];

我需要可以提取 %% 通配符在 $template 位置的文本的逻辑,以便 $strings 数组可用于生成以下内容:

$template = '%% is %% because %%.';
$result = [
    ['Cheese','the best thing','it is great on chips.'],
    ['My brother','my best friend','he\'s always been there.'], 
    ['Listen! Whosebug','how I am still employed','I am not afraid to ask for help.'],
    ['Derp... Why', 'it that I can\'t do this easily? Maybe it is','I need more practice.']
];

注意:将'explode'与物理words/letters一起使用不是一个很好的选择,因为有些词可能会多次出现,我们只想定位第一次出现这样的事件。

您可以使用正则表达式作为模板:

$template = '(.+?) is (.+?) because (.+?)';

foreach($strings as $string) {
    preg_match("/$template/", $string, $matches);
    $result[] = [$matches[1], $matches[2], $matches[3]];
    //or
    //$result[] = array_slice($matches, 1);
}

您想添加一些错误检查,以防没有匹配项,或者只有 1 或 2 个等。

如果出于某种原因您需要特定的模板命名法,那么:

$template = '%% is %% because %%.';
$template = str_replace('%%', '(.+?)', $template);