在数组php中拆分段落

时间:2019-03-02 17:40:04

标签: php arrays

我有这种形式的数组:

 $steps = array (0=> "the sentence one. the sentence two. the sentence three.",
            1=> "the sentence for. the sentence 5");

并且我想要一个像这样的数组$steps

 $steps = array (0 => "the sentence one.",
            1 => "the sentence two.",
             .
             .
             4 =>"the sentence for."
             );

我尝试使用explodeimplode,但没有成功。

2 个答案:

答案 0 :(得分:1)

您可以使用(?<=\.\s)(?=\w)正则表达式在现有数组中拆分字符串,然后使用foreach循环遍历所有匹配项,并继续将所有拆分后的字符串添加到数组中。检查此PHP代码,

$steps = array (0=> "the sentence one. the sentence two. the sentence three.",
        1=> "the sentence for. the sentence 5");
$arr = array();
foreach ($steps as $s) {
    $mat = preg_split('/(?<=\.\s)(?=\w)/', $s);
    foreach($mat as $m) {
        array_push($arr,$m);
    }
}
print_r($arr);

打印

Array
(
    [0] => the sentence one. 
    [1] => the sentence two. 
    [2] => the sentence three.
    [3] => the sentence for. 
    [4] => the sentence 5
)

这假定通过查看当前样本数据,在句点.后跟一个空格之后,新句子开始。如果您有包含各种形式的点的更复杂的样本数据,请发布您的样本,如果需要,我的解决方案也可以更新以容纳它们。

答案 1 :(得分:0)

让我知道这是否对您有用preg_split("/\. (?=[A-Z])/", join(" ", $steps));

您的目标数组:

$steps = array (
    0 => "The sentence one. The sentence two. The sentence three.",
    1 => "The sentence for. The sentence 5"
);

$steps_unified = preg_split("/\. (?=[A-Z])/", join(" ", $steps));

print_r ($steps_unified);

您将获得:

Array ( 
    [0] => The sentence one 
    [1] => The sentence two 
    [2] => The sentence three 
    [3] => The sentence for 
    [4] => The sentence 5 
)

如果我们使用适当的语法,则行应以'。'结尾。并以一个空格和一个大写的后缀词开头。