你会如何在每个第二个角色上爆炸一串文字?
$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);
有没有办法在拆分字符串时这样做,或者我必须将其爆炸,然后将其内爆以获得所需的效果?有任何建议怎么做?
谢谢!
编辑:
我想分开每隔一个点(。),而不是任何第二个角色。
答案 0 :(得分:2)
爆炸每个点,然后再将一对数组条目重新组合在一起
$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);
答案 1 :(得分:1)
我会使用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
行为将该部分添加到数组中(由split-separator分割),我们仍然得到了我们需要的东西,也是字符串的结尾部分。
注意:在我原来的回答中,我将\.|$
放在正则表达式的末尾,目的是识别字符串的最后一部分也作为分隔符,但如上所述,这不是必要。它没有|$
部分。
答案 2 :(得分:0)
如何使用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
);
这将在$ 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捕获的内容。其余的必须是其余部分:)