У меня есть такой абзац.
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
г. php воспринимает это как новую строку, поэтому plz дает любую идею или предложение, что я могу преобразовать абзац в строку, игнорирующую b. 13 May 1937
b. 13 May 1937
тип препятствия. PLZ помочь мне.
Вы можете просто использовать функцию preg_split
например, как
$result = preg_split('/\.+(?![^\(]*\))/',$str);
print_r(array_filter($result));
Объяснение (Regex):
\.+(?![^\(]*\))
\.+
соответствует символу .
буквально(?![^\(]*\))
Отрицательный Lookahead - не соответствовать тем символам, которые входят в () 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);
В принципе, регулярное выражение говорит о разделении строки на:
- Литерала "." (точка), то:
- Один или несколько алфавитов (это означает, что в массиве может быть строка, начинающаяся с чего-то типа "(13 мая..."). Затем:
- Символ пробела
Итак, на основе вашего кода вы должны иметь:
$line = preg_split("/\.[a-zA-Z]+ /", $para);
for ($i = 0; $i < count($line); ++$i) {
echo "<P>$i.$line[$i]</P>";
}