簡體   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