我有这样的段落。
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
php把它作为新行,所以plz给出任何想法或建议,我可以将段落转换成行忽略b. 13 May 1937
类型的障碍。
请帮助我。
答案 0 :(得分:1)
您可以像{/ p>一样使用preg_split
功能
$result = preg_split('/\.+(?![^\(]*\))/',$str);
print_r(array_filter($result));
解释(正则表达式):
\.+(?![^\(]*\))
\.+
字面匹配字符.
(?![^\(]*\))
否定前瞻 - 不匹配() parentheses
答案 1 :(得分:0)
这是一个使用正则表达式的工作解决方案:
$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);
基本上,正则表达式表示将字符串拆分为:
- 字面意思“。” (点),然后:
- 一个或多个字母,(这可以避免像字符串一样开头使用字符串的可能性(b。13月13日......)。然后:
- 空格字符
醇>
因此,根据您的代码,您应该:
$line = preg_split("/\.[a-zA-Z]+ /", $para);
for ($i = 0; $i < count($line); ++$i) {
echo "<P>$i.$line[$i]</P>";
}