繁体   English   中英

如何在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 1937在这里,因为b. 13 may 1937 b. 13 may 1937 ,PHP将其作为新行,因此plz给出了任何我可以将段落转换为忽略b. 13 May 1937行的想法或建议b. 13 May 1937 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>";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM