简体   繁体   中英

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

I have paragraph like this.

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.

I want to convert paragraph(string) into line(array) like this

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.

So i create code like this

 <?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>";

  }
?>

it works without error but it give the output like this

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.

I want the output as each sentence as new line.here because the b. 13 may 1937 b. 13 may 1937 the php take it as new line so plz give any idea or suggestion that i can convert the paragraph into line ignoring b. 13 May 1937 b. 13 May 1937 type of hurdle. plz help me.

You can simply use preg_split function like as

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

Explanation(Regex) :

\.+(?![^\(]*\))
  • \\.+ matches the character . literally
  • (?![^\\(]*\\)) Negative Lookahead - Not to match those characters that comes within () parentheses

Demo

Here's a working solution, using regular expression:

$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);

Basically, the regular expression says to split the string at:

  1. A literal "." (dot), then:
  2. One or more alphabets, (This elemintates the possibility of having strings like beginning with something like "(b. 13 May..." in the array. Then:
  3. A space character

So, based on your code, you should have:

$line =  preg_split("/\.[a-zA-Z]+ /", $para);
for ($i = 0; $i < count($line); ++$i) {
   echo "<P>$i.$line[$i]</P>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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