如何在PHP中将段落(字符串)转换为行(数组)


How to Convert Paragraph(string) to line (array) in PHP?

我有这样一段。

Rose Helen (b. 13 May 1937), married The Lord Luce.Laura Violet (b. 18 January 1939), married Sir John Montgomery-Cuninghame of Corsehill, 12th Baronet. Emma Harriet (b. 16 October 1941), married Sir Michael Harris Caine.Harriet Mary (b. 29 June 1946), married Charles Hugh Flower (a maternal great-great-grandson of the 1st Duke of Abercorn). As Nicholson had no sons from his marriage, his title became extinct upon his death in 1991.

我想把段落(字符串)转换成行(数组),像这样

1.Rose Helen (b. 13 May 1937), married The Lord Luce.
2.Laura Violet (b. 18 January 1939), married Sir John Montgomery-Cuninghame of Corsehill, 12th Baronet.
3.Emma Harriet (b. 16 October 1941), married Sir Michael Harris Caine.
4.Harriet Mary (b. 29 June 1946), married Charles Hugh Flower (a maternal great-great-grandson of the 1st Duke of Abercorn).
5.As Nicholson had no sons from his marriage, his title became extinct upon his death in 1991.

所以我创建了这样的代码

 <?PHP
$para="Rose Helen (b. 13 May 1937), married The Lord Luce.Laura Violet (b. 18 January 1939), married Sir John Montgomery-Cuninghame of Corsehill, 12th Baronet.Emma Harriet (b. 16 October 1941), married Sir Michael Harris Caine.Harriet Mary (b. 29 June 1946), married Charles Hugh Flower (a maternal great-great-grandson of the 1st Duke of Abercorn).As Nicholson had no sons from his marriage, his title became extinct upon his death in 1991.";

$line = explode(". ",$para);
  for ($i = 0; $i < count($line); ++$i) {
  echo "<P>$i.$line[$i]</P>";
  }
?>

没有错误,但是输出如下

0.Rose Helen (b
1.13 May 1937), married The Lord Luce.Laura Violet (b
2.18 January 1939), married Sir John Montgomery-Cuninghame of Corsehill, 12th Baronet.Emma Harriet (b
3.16 October 1941), married Sir Michael Harris Caine.Harriet Mary (b
4.29 June 1946), married Charles Hugh Flower (a maternal great-great-grandson of the 1st Duke of Abercorn).As Nicholson had no sons from his marriage, his title became extinct upon his death in 1991.

我希望输出的每个句子都是新的一行。这是因为b. 13 may 1937php把它当作新的行,所以请给出任何想法或建议,我可以将段落转换成行,忽略b. 13 May 1937类型的障碍。

您可以简单地使用preg_split函数,如

$result = preg_split('/'.+(?![^'(]*'))/',$str);
print_r(array_filter($result));

解释(正则表达式):

'.+(?![^'(]*'))
  • '.+匹配字符.字面上
  • (?![^'(]*'))负向前看-不匹配() parentheses
  • 中的字符

下面是一个工作解决方案,使用正则表达式:

$para="Rose Helen (b. 13 May 1937), married The Lord Luce.Laura Violet (b. 18 January 1939), married Sir John Montgomery-Cuninghame of Corsehill, 12th Baronet.Emma Harriet (b. 16 October 1941), married Sir Michael Harris Caine.Harriet Mary (b. 29 June 1946), married Charles Hugh Flower (a maternal great-great-grandson of the 1st Duke of Abercorn).As Nicholson had no sons from his marriage, his title became extinct upon his death in 1991.";
$split_arr = preg_split("/'.[a-zA-Z]+ /", $para);
var_dump($split_arr);

基本上,正则表达式表示将字符串分割为:

  1. 字面意思的"。"(点),然后:
  2. 一个或多个字母,这消除了数组中以"(b. 13 May…)"开头的字符串的可能性。:
  3. 空格字符
所以,根据你的代码,你应该有:
$line =  preg_split("/'.[a-zA-Z]+ /", $para);
for ($i = 0; $i < count($line); ++$i) {
   echo "<P>$i.$line[$i]</P>";
}