<p>Hey so what I want to do is snag the content for the first paragraph. The string <code>$blog_post</code> contains a lot of paragraphs in the following format:</p>

<pre><code>&lt;p&gt;Paragraph 1&lt;/p&gt;&lt;p&gt;Paragraph 2&lt;/p&gt;&lt;p&gt;Paragraph 3&lt;/p&gt;
</code></pre>

<p>The problem I'm running into is that I am writing a regex to grab everything between the first <code>&lt;p&gt;</code> tag and the first closing <code>&lt;/p&gt;</code> tag. However, it is grabbing the first <code>&lt;p&gt;</code> tag and the <strong>last</strong> closing <code>&lt;/p&gt;</code> tag which results in me grabbing everything.</p>

<p>Here is my current code:</p>

<pre><code>if (preg_match("/[\\s]*&lt;p&gt;[\\s]*(?&lt;firstparagraph&gt;[\\s\\S]+)[\\s]*&lt;\\/p&gt;[\\s\\S]*/",$blog_post,$blog_paragraph))
   echo "&lt;p&gt;" . $blog_paragraph["firstparagraph"] . "&lt;/p&gt;";
else
  echo $blog_post;
</code></pre>

[{'answer_id': 38696, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 1, 'selected': False, 'text': '<p>It would probably be easier and faster to use <a href="http://php.net/strpos" rel="nofollow noreferrer">strpos()</a> to find the position of the first</p>\n\n<pre><code> &lt;p&gt;\n</code></pre>\n\n<p>and first</p>\n\n<pre><code>&lt;/p&gt;\n</code></pre>\n\n<p>then use <a href="http://php.net/substr" rel="nofollow noreferrer">substr()</a> to extract the paragraph.</p>\n\n<pre><code> $paragraph_start = strpos($blog_post, \'&lt;p&gt;\');\n $paragraph_end = strpos($blog_post, \'&lt;/p&gt;\', $paragraph_start);\n $paragraph = substr($blog_post, $paragraph_start + strlen(\'&lt;p&gt;\'), $paragraph_end - $paragraph_start - strlen(\'&lt;p&gt;\'));\n</code></pre>\n\n<p><strong>Edit:</strong> Actually the regex in others\' answers will be easier and faster... your big complex regex in the question confused me...</p>\n'}, {'answer_id': 38697, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 5, 'selected': True, 'text': "<p>Well, sysrqb will let you match anything in the first paragraph assuming there's no other html in the paragraph. You might want something more like this</p>\n\n<pre><code>&lt;p&gt;.*?&lt;/p&gt;\n</code></pre>\n\n<p>Placing the <code>?</code> after your <code>*</code> makes it non-greedy, meaning it will only match as little text as necessary before matching the <code>&lt;/p&gt;</code>.</p>\n"}, {'answer_id': 38847, 'author': 'Erik Öjebo', 'author_id': 276, 'author_profile': 'https://Stackoverflow.com/users/276', 'pm_score': 3, 'selected': False, 'text': '<p>If you use <code>preg_match</code>, use the <strong>"U"</strong> flag to make it un-greedy.</p>\n\n<pre><code>preg_match("/&lt;p&gt;(.*)&lt;\\/p&gt;/U", $blog_post, &amp;$matches);\n</code></pre>\n\n<p><code>$matches[1]</code> will then contain the first paragraph.</p>\n'}, {'answer_id': 47850655, 'author': 'eLRuLL', 'author_id': 858913, 'author_profile': 'https://Stackoverflow.com/users/858913', 'pm_score': 0, 'selected': False, 'text': "<p>Using Regular Expressions for html parsing is never the right solution. You should be using XPATH for this particular case:</p>\n\n<pre><code>$string = &lt;&lt;&lt;XML\n&lt;a&gt;\n &lt;b&gt;\n  &lt;c&gt;texto&lt;/c&gt;\n  &lt;c&gt;cosas&lt;/c&gt;\n &lt;/b&gt;\n &lt;d&gt;\n  &lt;c&gt;código&lt;/c&gt;\n &lt;/d&gt;\n&lt;/a&gt;\nXML;\n\n$xml = new SimpleXMLElement($string);\n\n/* Busca &lt;a&gt;&lt;b&gt;&lt;c&gt; */\n$resultado = $xml-&gt;xpath('//p[1]');\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/38691', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/428190/']