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.
I want to get paragraphs by limit. I know I am able to limit my results by preg_match_all.
I have two struggles:
- Paragraphs are "created" by html editor, so attributes are attached sometimes
- If it is possible, i want the
<p>
too, but only get the text is good too
For example:
<p>Paragraph 1</p>
<p attribute="value">Paragraph 2</p>
When I limit on one, I want only the first paragraph, but limit 2 should return paragraph 2 too, even it contains attributes.
What I tried:
function GetParagraph($content, $limitParagraph = 1)
{
preg_match_all('~(<p>(.+?)</p>){' . (int)$limitParagraph. '}~i', $sHTML, $aMatches);
return $aMatches[0];
}
Also regex with '~(<p(.*?)>(.+?)</p>){' . (int)$limitParagraph. '}~i'
didn't work well