开发者

Getting the string inside of a the first paragraph in a blogpost

开发者 https://www.devze.com 2023-03-14 15:01 出处:网络
Imagine my string is $str = \"<开发者_Go百科p>paragraph 1</p><p>paragraph 2</p><p>paragraph 3</p>\";

Imagine my string is

$str = "<开发者_Go百科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?


$dom = new DOMDocument();
$dom->loadHTML($str);

$xp = new DOMXPath($dom);

$res = $xp->query('//p');

$firstParagraph = $res[0]->nodeValue;


I'm typing this up quickly, but wouldn't:

$str = "<p>paragraph 1</p><p>paragraph 2</p><p>paragraph 3</p>";
preg_match('/<p>(.*?)<\/p>/i', $str, $paragraphs);
echo $paragraphs[0]; // Paragraph 1

work more efficiently than these other solutions?


You are asking two things. But with

$array = preg_split('#</p><p>|<p>|</p>#i', $string, -1, PREG_SPLIT_NO_EMPTY)

you can split the paragraphs into an array, $string being your string with the paragraphs.

To get the first paragraph only, use:

preg_match('#^<p>(.*?)</p>#i', $string, $matches)

$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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号