title: Regex for limit paragraphs
body: <p>The question is asked multiple times, but the snippets I found didnt work well. I have less experience with regex so I hope you can help me.</p>
  
  <p>I want to get paragraphs by limit.
  I know I am able to limit my results by preg_match_all.</p>
  
  <p>I have two struggles:</p>
  
  <ul>
  <li>Paragraphs are "created" by html editor, so attributes are attached sometimes</li>
  <li>If it is possible, i want the <code>&lt;p&gt;</code> too, but only get the text is good too</li>
  </ul>
  
  <p>For example:</p>
  
  <pre><code>&lt;p&gt;Paragraph 1&lt;/p&gt;
  &lt;p attribute="value"&gt;Paragraph 2&lt;/p&gt;
  </code></pre>
  
  <p>When I limit on one, I want only the first paragraph, but limit 2 should return paragraph 2 too, even it contains attributes.</p>
  
  <p>What I tried:</p>
  
  <pre><code>function GetParagraph($content, $limitParagraph = 1)
  {
      preg_match_all('~(&lt;p&gt;(.+?)&lt;/p&gt;){' . (int)$limitParagraph. '}~i', $sHTML, $aMatches);
      return $aMatches[0];
  }
  </code></pre>
  
  <p>Also regex with <code>'~(&lt;p(.*?)&gt;(.+?)&lt;/p&gt;){' . (int)$limitParagraph. '}~i'</code> didn't work well</p>