我花了最后一个小时寻找回复,但我还没有找到任何回复,所以我在这里问...
我需要一种方法(当然是正则表达式,但其他所有内容(如爆炸都可以)将如下所示的句子切成同一数组中的部分:
这是第一部分,这是第二部分;这是第三部分!这是第四部分?一次又一次,直到句子结束。
我想要一个包含以下条目的数组(请不要在标点符号后面或前面加上空格):
- [0] => "这是第一部分"
- [1] => "这是第二部分"
- [2] => "这是第三部分"
- [3] => "这是第四部分"
- [4] => "再次"
- [5] => "再说一遍"
- [6] => "直到判决结束"
编辑:抱歉,以下示例是英文的,但它应该能够处理各种脚本(基本上都是Unicode)。
多谢!
我在这里找到了解决方案
这是我使用多个分隔符进行爆炸输出的方法。
<?php
//$delimiters has to be array
//$string has to be array
function multiexplode ($delimiters,$string) {
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);
print_r($exploded);
//And output will be like this:
// Array
// (
// [0] => here is a sample
// [1] => this text
// [2] => and this will be exploded
// [3] => this also
// [4] => this one too
// [5] => )
// )
?>
单个
preg_split
就可以完成这项工作:
$s = 'This is the first part, this is the second part; this is the third part! this is the fourth part? again - and again - until the sentence is over.';
print_r(preg_split('/s*[,:;!?.-]s*/u', $s, -1, PREG_SPLIT_NO_EMPTY));
输出:
Array
(
[0] => This is the first part
[1] => this is the second part
[2] => this is the third part
[3] => this is the fourth part
[4] => again
[5] => and again
[6] => until the sentence is over
)
尝试使用这个
$parts = preg_split("/[^A-Zs]+/i", $string);
var_dump($parts);