AA_Haider Posted December 25, 2012 Share Posted December 25, 2012 I had a multiple line text I want to get only first paragraph.like ......................................… <p>this is a paragraph number 1</p> <p>this is a paragraph number 2</p> ......................................… I want get only .................................. this is a paragraph number 1 ................................ How can I do that with RegExp. Please tell me. Link to comment Share on other sites More sharing options...
Jessica Posted December 25, 2012 Share Posted December 25, 2012 It's better to use a Dom parser. Link to comment Share on other sites More sharing options...
AA_Haider Posted December 26, 2012 Author Share Posted December 26, 2012 (edited) I have a these regex and I am using preg_match function /(<p[^>]*>.*?<\/p>)/m but this is not working I did not know why But if paragraphs are a single line form then it will work fine. I need it for multiply lines. Edited December 26, 2012 by AA_Haider Link to comment Share on other sites More sharing options...
Christian F. Posted December 26, 2012 Share Posted December 26, 2012 As Jessica said, you'll want to use DOMdocument or another DOM parser for this. Link to comment Share on other sites More sharing options...
DavidAM Posted December 27, 2012 Share Posted December 27, 2012 While I agree with Jessica that a DOM parser is the best way to go, there are a couple of things you can try with the regexp. 1) The dot in your pattern will NOT match a newline unless you add the "s" modifier. 2) Your pattern will not match a paragraph tag that is in uppercase, unless you use the "i" modifier. 3) Since you are not trying to anchor at the beginning or end of the string, you do NOT really need the "m" modifier. 4) Since you are looking for the match of the entire pattern, you do not need the capturing parenthesis. /<p[^>]*>.*?<\/p>/si might give you better luck. P.S. If you are attempting to scrape a site without permission of the site owner, please ignore my advice and repent from your evil ways. Link to comment Share on other sites More sharing options...
Recommended Posts