每隔一个点字符分解一个字符串

Explode a string on every second dot character

你如何在每隔一个字符上分解一串文本?

$text = "Online groceries is one of the few channels to market that is growing, though its profitability is questionable. According to industry research group IGD the UK online grocery market will nearly double to 18 billion pounds in the five years to 2020.Online groceries is one of the few channels to market that is growing, though its profitability is questionable. According to industry research group IGD the UK online grocery market will nearly double to 18 billion pounds in the five years to 2020.";

要在每个点上爆炸,我会使用:

$domain_fragmented = explode(".", $text);

有没有办法在拆分字符串时执行此操作,或者我必须将其爆炸然后内爆才能获得所需的效果?有什么建议吗?

谢谢!

编辑:

我想每隔一个点 (.) 进行拆分,而不是任何第二个字符。

在每个点上展开,然后将成对的数组条目重新组合在一起

$string = 'ab.cd.ef.gh.ij.kl.mn.op.qr';

$split = array_map(
    function($value) {
        return implode('.', $value);
    },
    array_chunk(explode('.', $string), 2)
);

var_dump($split);

Demo

使用 pre_match_all 怎么样?

/(?:(?:.+?\.){2})/

有了这个,你会得到一个列表,每个列表项包含两个句子(用点分隔)。

$matches = null;
$returnValue = preg_match_all(
    '/(?:(?:.+?\.){2})/',
    '
        1_Online groceries is one of the few channels to market that is growing, though its profitability is questionable. 
        2_According to industry research group IGD the UK online grocery market will nearly double to 18 billion pounds in the five years to 2020.
        3_Online groceries is one of the few channels to market that is growing, though its profitability is questionable. 
        4_According to industry research group IGD the UK online grocery market will nearly double to 18 billion pounds in the five years to 2020.',
    $matches
);

这 returns $匹配以下内容:

array (
    0 => array (
        0 => '1_Online groceries is one of the few channels to market that is growing, though its profitability is questionable. 2_According to industry research group IGD the UK online grocery market will nearly double to 18 billion pounds in the five years to 2020.',
        1 => '3_Online groceries is one of the few channels to market that is growing, though its profitability is questionable. 4_According to industry research group IGD the UK online grocery market will nearly double to 18 billion pounds in the five years to 2020.',
    )
)

遗憾的是,这并不完美。我没有成功捕获任何悬在输入字符串末尾的附加文本(例如 "sentence1.sentence2.bla")。所以,如果你(或其他人)不能想出一个改进的正则表达式来捕获这个(也就是说,你是否需要捕获这个;如果你知道输入字符串总是由成对的句子组成,那么一切都很好)你可能想 trim 去掉用 pre_match_all 捕获的内容。因此,其余的必须是其余的:)

我会使用 preg_split 如下:

$test = "this.is.a.test.to.see.how.preg_split.can.be.used.for.this";
$res = preg_split ("/(.*?\..*?)\./", $test, NULL,
        PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($res);

输出

Array (
    [0] => this.is
    [1] => a.test
    [2] => to.see
    [3] => how.preg_split
    [4] => can.be
    [5] => used.for
    [6] => this
) 

说明

这里使用的正则表达式捕获直到并包括第二个点的文本,并且有一个排除第二个点的捕获组。通过指定 PREG_SPLIT_DELIM_CAPTURE 选项,此捕获组的所有匹配项都包含在结果中。

由于原始字符串中的每个字符都会以某种方式成为拆分表达式的一部分,因此实际上 split 的正常结果的其余部分都是空的,可能除了结尾部分字符串。通过使用 PREG_SPLIT_NO_EMPTY 选项,这些空字符串被排除在结果之外。

字符串的结尾部分没有被正则表达式捕获,因为最后的点会丢失。但是由于正常的 split 行为将该部分添加到数组中(被拆分分隔符拆分),我们仍然得到我们需要的,也包括字符串的结尾部分。

注意:在我原来的回答中,我把 \.|$ 放在了正则表达式的末尾,目的是识别字符串的最后一部分也作为分隔符,但是如上所述,这个没有必要。它在没有 |$ 部分的情况下工作。