Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 每隔一个点字符分解一个字符串_Php_Split_Explode_String Split - Fatal编程技术网

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

Php 每隔一个点字符分解一个字符串,php,split,explode,string-split,Php,Split,Explode,String Split,如何在每一个字符上分解一个文本字符串 $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

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

$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);

使用pre\u match\u 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
);
这将以$matches形式返回以下内容:

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”)。因此,如果您(或其他人)不能拿出一个改进的正则表达式来捕获这个(也就是说,您需要捕获这个;如果您知道输入字符串总是由成对的句子组成,那么一切都已经很好了),那么您可能希望删除使用pre_match_all捕获的内容。因此,剩余部分必须是剩余部分:)

我将使用
preg\u 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\u SPLIT\u DELIM\u CAPTURE
选项,此捕获组的所有匹配项都包含在结果中

由于原始字符串中的每个字符都会以某种方式成为拆分表达式的一部分,因此剩余部分(实际上是
拆分的正常结果)都是空的,除了字符串的结尾部分。通过使用
PREG\u SPLIT\u NO\u EMPTY
选项,这些空字符串将从结果中排除

正则表达式不会捕获字符串的结尾部分,因为最后一个点将丢失。但是,当正常的
split
行为将该部分添加到数组中(被拆分分隔符拆分)时,我们仍然可以得到所需的内容,字符串的结尾部分也是如此


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

你能解释一下正则表达式中的|$吗?我知道这些术语,但我不知道你为什么在那里用这个。我运行的所有测试在没有它的情况下都能正常工作。你是对的,你可以删除它(和管道一起)。它将给出相同的结果,但在某些情况下通过不同的路径。<代码> $代表“字符串的结尾”,并且想法是考虑匹配很好,即使没有第二个点,但是只有当你在字符串的结尾。没有
$
就没有最后一个“组”的匹配项,但这仍然是可以的,因为preg_split会将其视为正常分割的结果(不是空的),并将其包括在内。因此,在这两种情况下,最终结果是相同的。考虑到这一点,我将把它从我的答案中删除。谢谢谢谢你的建议。将使用explode/infrade函数,因为它工作良好且更易于实现