获取博客文章第一段中的字符串
想象一下我的字符串是
$str = "<p>paragraph 1</p><p>paragraph 2</p><p>paragraph 3</p>";
,我想得到......
$firstParagraph = "paragraph 1"
最好的方法是什么?
是否可以轻松地将每个段落拆分为一个数组?
Imagine my string is
$str = "<p>paragraph 1</p><p>paragraph 2</p><p>paragraph 3</p>";
and I want to get...
$firstParagraph = "paragraph 1"
What would be the best way of doing that?
Is it possible to split each paragraph into an array easily?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在快速输入此内容,但不会:
比其他解决方案更有效地工作吗?
I'm typing this up quickly, but wouldn't:
work more efficiently than these other solutions?
你问的是两件事。但是,
您可以将段落拆分为一个数组,$string 是带有段落的字符串。
要仅获取第一段,请使用:
$matches 将包含您想要的内容。与之前的正则表达式相比,这甚至具有更高的内存效率和更快的速度。如果我见过的话,我不应该提及使用面向对象的 xml 解析器的解决方案完全是大材小用。
You are asking two things. But with
you can split the paragraphs into an array, $string being your string with the paragraphs.
To get the first paragraph only, use:
$matches will contain what you want. And this is even more memory efficient and quicker than the previous regex. I shouldn't have to mention that solutions with object oriented xml parsers are total overkills if I've ever seen one.