PHP - preg_split html 按换行符或段落
我有一些像这样的html:
<p>Lorem ipsum</p>
Dolor sit amet
<br><br>
consectetur
<p>adipiscing</p>
<br>elit.
我试图将它分成两部分,最后得到这个:
Array(
[0] => <p>Lorem ipsum</p>
Dolor sit amet
[1] => <br><br>
consectetur
<p>adipiscing</p>
<br>elit.
)
这个想法是让第一个数组元素包含前两个“段落”。我所说的“段落”是指“p”标签内的文本,或者两个连续的“br”标签之后的文本。
I have some html like this:
<p>Lorem ipsum</p>
Dolor sit amet
<br><br>
consectetur
<p>adipiscing</p>
<br>elit.
And I'm trying to split it in two parts and in the end, to get this:
Array(
[0] => <p>Lorem ipsum</p>
Dolor sit amet
[1] => <br><br>
consectetur
<p>adipiscing</p>
<br>elit.
)
The idea is to have the first array element containing either the first two "paragraphs". By "paragraph" I mean, either the text inside a "p" tag, or the text after two consecutive "br" tags.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这个模式:
/(<\s*p\s*\/?>)|(<\s*br\s*\/?>)/
会对你有帮助?请参阅演示Maybe this pattern:
/(<\s*p\s*\/?>)|(<\s*br\s*\/?>)/
would help you? See the demo您需要根据字符串的外观将字符串拆分。 preg_split 将按正则表达式进行拆分,尽管这是可能的,但我怀疑这不是您所需要的。
这是由两个
标签分割这将输出为数组
You need to split a string by a string by the looks of it. preg_split will split by a regular expression, although it is possible, I suspect it's not what you need.
Here's Splitting by two
<br>
tagsThis will output as an array
不能 100% 确定 s 模式修饰符是必要的,但这应该可以工作,并捕获指定的第二个值中的
。
Not 100% sure the s pattern modifier is necessary, but this should work, and capture the
<br><br>
in the 2nd value as specified.