php 从以一个关键字数组开头的文本中获取子字符串,并且该子字符串不能包含第二个关键字

j9per5c4  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(96)

我想写一些函数,接受两个参数$text$keys。数组中的键.
在输出中,我们需要得到一个数组,其中的键将是传递给函数的键(如果我们在文本中找到它们),值将是这个键之后的文本,直到它遇到下一个键或文本结束。如果键在文本中重复,则只将最后一个值写入数组

例如:

  • 可视化文本:* 存有只是印刷和排版行业的一个虚拟文本。存有一直是行业的标准假人自从16世纪的文本。
$text = 'Lorem Ipsum is simply one dummy text of the printing and  two typesetting industry. Lorem Ipsum has been the industry\'s one standard dummy text ever since the three 1500s.';

$keys = ['one', 'two', 'three'];

预期输出:

[
    'one' => 'standard dummy text ever since the',
    'two' => 'typesetting industry. Lorem Ipsum has been the industry\'s',
    'three' => '1500s.'
]

我试着写一个正则表达式来科普这个任务,但没有成功。
最后一次尝试:

function getKeyedSections($text, $keys) {
    $keysArray = explode(',', $keys);
    $pattern = '/(?:' . implode('|', array_map('preg_quote', $keysArray)) . '):\s*(.*?)(?=\s*(?:' . implode('|', array_map('preg_quote', $keysArray)) . '):\s*|\z)/s';
    preg_match_all($pattern, $text, $matches);

    $keyedSections = [];
    foreach ($keysArray as $key) {
        foreach ($matches[1] as $index => $value) {
            if (stripos($matches[0][$index], $key) !== false) {
                $keyedSections[trim($key)] = trim($value);
                break;
            }
        }
    }

    return $keyedSections;
}
x6yk4ghg

x6yk4ghg1#

这里有一个preg_match_all()的方法,它提取了从任何键开始到任何键之前结束的所有段。array_column()调用只是为了后面的匹配而丢弃前面的匹配,并设置所需的关联结果。(Demo

$text = "Lorem Ipsum is simply one dummy text of the printing and  two typesetting industry. Lorem Ipsum has been the industry's one standard dummy text ever since the three 1500s.";

$keys = ['one', 'two', 'three'];

$escaped = implode('|', array_map('preg_quote', $keys));

preg_match_all('#\b(' . $escaped . ')\b\s*\K.*?(?=\s*(?:$|\b(?:' . $escaped . ')\b))#', $text, $m, PREG_SET_ORDER);

var_export(array_column($m, 0, 1));

输出量:

array (
  'one' => 'standard dummy text ever since the',
  'two' => 'typesetting industry. Lorem Ipsum has been the industry\'s',
  'three' => '1500s.',
)

相关问题