php拆词,PHP-使用正则表达式进行深度(无限)拆分单词

可以说我有:

$line = "{This is my {sentence|words} I wrote.}"

输出:

This is my sentence I wrote.

This is my words I wrote.

但是,正则表达式应匹配深层和嵌套值并拆分这些值,例如:

$line = "{This is my {sentence|words} I wrote on a {sunny|cold} day.}";

输出:

This is my sentence I wrote on a sunny day.

This is my sentence I wrote on a cold day.

This is my words I wrote on a sunny day.

This is my words I wrote on a cold day.

我的第一个操作是像下面的代码那样过度爆炸,但是结果不合适:

$res = explode("|", $line);

忠告?谢谢.

编辑:这些行中的东西:

$line = "{This is my {sentence|words} I wrote on a {sunny|cold} day.}";

$regex = "{[^{}]*}";

$match = [];

preg_match($regex, $line, $match);

var_dump($match);

正如已经说过的,它可以达到无穷无尽的极限,这在for循环中是适当的.

解决方法:

看一下这个.我通过用%s替换您的模式并使用vsprintf,然后递归地遍历匹配项来完成它.

我在代码中添加了很多注释…了解递归通常是一件很麻烦的事.

$line = "{This is my {sentence|statement} I {wrote|typed} on a {hot|cold} {day|night}.}";

$matches = getMatches($line);

printWords([], $matches, $line);

// function to find patterns in the line. Takes $line by reference to replace pattern matches with a vsprintf placeholder

function getMatches(&$line) {

// remove beginning and trailing brackets on the main sentence

$line = trim($line, '{}');

// initialize variable that will hold the list of pattern matches

$matches = null;

// look for an opening curly brace and skip everything until the ending curly brace

$pattern = '/\{[^}]+\}/';

// find all matches and put them in $matches

preg_match_all($pattern, $line, $matches);

// preg_match_all nests one level deeper than we need

$matches = $matches[0];

// replace all matches with a %s placeholder

$line = preg_replace($pattern, '%s', $line);

// split each of the matches by vertical pipe

foreach ($matches as $index => $match) {

$matches[$index] = explode('|', trim($match, '{}'));

}

return $matches;

}

// recursive function. $args will be used as the second argument to vsprintf

function printWords(array $args, array $matches, $line) {

// get the first element in the array of $matches, remove it from the array

$current = array_shift($matches);

// keep track of the current $args index for this recursive iteration

$currentArgIndex = count($args);

// loop through each of the words in the current set of matches

foreach ($current as $word) {

// update $args and set the vsprintf argument at this iteration's position to the next word in the set of words

$args[$currentArgIndex] = $word;

if (!empty($matches)) {

// repeat this process (recursively) until we are at the end of the list of matches

printWords($args, $matches, $line);

} else {

// if this is the last match in the line, echo the sentence with all args from previous recursive iterations added

echo vsprintf($line, $args) . '
';

}

}

}

输出:

This is my sentence I wrote on a hot day.

This is my sentence I wrote on a hot night.

This is my sentence I wrote on a cold day.

This is my sentence I wrote on a cold night.

This is my sentence I typed on a hot day.

This is my sentence I typed on a hot night.

This is my sentence I typed on a cold day.

This is my sentence I typed on a cold night.

This is my statement I wrote on a hot day.

This is my statement I wrote on a hot night.

This is my statement I wrote on a cold day.

This is my statement I wrote on a cold night.

This is my statement I typed on a hot day.

This is my statement I typed on a hot night.

This is my statement I typed on a cold day.

This is my statement I typed on a cold night.

标签:explode,split,php,regex

来源: https://codeday.me/bug/20191118/2031486.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值