如何在PHP中将段落(字符串)转换为行(数组)?
我有这样的段落如何在PHP中将段落(字符串)转换为行(数组)?,php,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 J
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>";
}
?>
所以我创建了这样的代码
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>";
}
?>
我希望输出的每个句子都是新行。1937年5月13日
php将其作为新行,因此请给出任何想法或建议,我可以将该段落转换为忽略b行。1937年5月13日
跨栏类型。
请帮帮我。这里有一个有效的解决方案,使用正则表达式:
$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);
基本上,正则表达式表示在以下位置拆分字符串:
$line = preg_split("/\.[a-zA-Z]+ /", $para);
for ($i = 0; $i < count($line); ++$i) {
echo "<P>$i.$line[$i]</P>";
}
$line=preg\u split(“/\.[a-zA-Z]+/”,$para);
对于($i=0;$i$i.$line[$i]”;
}
您可以简单地使用preg\u split
函数,如
$result = preg_split('/\.+(?![^\(]*\))/',$str);
print_r(array_filter($result));
解释(正则表达式):
\.+(?![^\(]*\))
逐字匹配字符\.+
负前瞻-与(?![^\(]*\)
()括号内的字符不匹配
句子作为新行,那么为什么不在新行上拆分呢?对不起,这是我的错误,它是连续行,没有换行符…现在我编辑并更正它。在句子末尾的
后面没有空格。但是中的
后面确实有空格(b.1937年5月13日)
。所以explode
会在这些日期将其拆分。是的,它们有空间。我尝试了“.”和“.”。没有任何效果。你是天才。你救了我的命。上帝保佑你。上帝给你生命中所有的幸福它只与()如何处理像etci博士这样的所有类型,如果段落转换为行忽略这种类型的错误,