我正在寻找最有效的解决方案,它将使我能够构建一个字符串模板,以从使用模板结构的句子中提取单词和短语。
假设我的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! StackOverflow 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! StackOverflow','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.']
];
注:对物理单词/字母使用“爆炸”不是一个好选择,因为某些单词可能多次出现,而我们只想针对此类事件中的第一个出现。
您可以使用正则表达式作为模板:
$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);