[英]Convert multiple new lines to paragraphs
我想從一個字符串中找到段落,並將其格式化,我有什么樣的作品,但它不能100%工作。
所以,我有這個看起來像這樣的字符串:
##Chapter 1
Once upon a time there was a little girl named sally, she went to school.
One day it was awesome!
##Chapter 2
We all had a parade!
我正在通過將##...
轉換為<H2>
來格式化字符串,現在看起來像這樣:
<h2>Chapter 1</h2>
Once upon a time there was a little girl named sally, she went to school.
One day it was awesome!
<h2>Chapter 2</h2>
We all had a parade!
現在,我想將所有內容都轉換為一個段落,然后執行以下操作:
// Converts sections to paragraphs:
$this->string = preg_replace("/(^|\n\n)(.+?)(\n\n|$)/", "<p>$2</p>", $this->string);
// To Remove paragraph tags from header tags (h1,h2,h3,h4,h5,h6,h7):
$this->string = preg_replace("/<p><h(\d)>(.+?)<\/h\d><\/p>/i", "<h$1>$2</h$1>", $this->string);
這是最終輸出(為便於閱讀,添加了新行):
<h2>Chapter 1</h2>
Once upon a time there was a little girl named sally, she went to school.
<p>One day it was awesome!</p>
<h2>Chapter 2</h2>
<p>We all had a parade!</p>
正如我在開始時所說的那樣,這並不能100%起作用,並且您可以看到沒有在第一段中添加一段。 我該怎么做才能改善正則表達式?
您可以一步完成:
$this->string = preg_replace('~(*BSR_ANYCRLF)\R\R\K(?>[^<\r\n]++|<(?!h[1-6]\b)|\R(?!\R))+(?=\R\R|$)~u',
'<p>$0</p>', $this->string);
圖案細節
(*BSR_ANYCRLF) # \R can be any type of newline
\R\R # two newlines
\K # reset the match
(?> # open an atomic group
[^<\r\n]++ # all characters except <, CR, LF
| # OR
<(?!h[1-6]\b) # < not followed by a header tag
| # OR
\R(?!\R) # single newline
)+ # close the atomic group and repeat one or more times
(?=\R\R|$) # followed by to newlines or the end of the string
將m開關添加到第一個正則表達式。
// Converts sections to paragraphs:
$this->string = preg_replace("/(^|\n\n)(.+?)(\n\n|$)/m", "<p>$2</p>", $this->string);
// To Remove paragraph tags from header tags (h1,h2,h3,h4,h5,h6,h7):
$this->string = preg_replace("/<p><h(\d)>(.+?)<\/h\d><\/p>/i", "<h$1>$2</h$1>", $this->string);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.