Forum Moderators: coopster

Message Too Old, No Replies

Regex Problem

pull out two paragraphs into separate variables

         

tolachi

8:08 am on May 17, 2004 (gmt 0)

10+ Year Member



I want to use ereg() or preg_match (or whatever works) to separate out the first paragraph of a chunk of html which would be everything between the first set of <p></p> tags, including the opening and closing p tags and any other inline html tags they contain. In the second substring I want to get everything after the first paragraph. So something like

ereg("(paragraph_1)(paragraph_2)",$var,$regs);

Mil gracias if anyone can help me out here.

mykel79

9:21 am on May 17, 2004 (gmt 0)

10+ Year Member



This seem to do the trick:
preg_match('/<p>(.*?)<\/p><p>(.*?)<\/p>/',$text,$temp);
echo 'part1=',$temp[1],'<br><br>';
echo 'part2=',$temp[2];

Netizen

10:33 am on May 17, 2004 (gmt 0)

10+ Year Member



How about:

preg_match("/^.*?(<p>.*?<\/p>)(.*?)$/",$input,$matches);

$first=$matches[1];
$rest=$matches[2];

The termination of the regex should be different if you want everything up to the ending </html> tag or some other limitation.

tolachi

10:46 am on May 17, 2004 (gmt 0)

10+ Year Member



Thanks. mykel79 I think that white space caused what you posted to not work for me. I ended up using this:

preg_match('/(<p>.*?<\/p>)[\t\n\s]*(.*)/',$this->description, $regs);

Netizen. Thanks for the post, but I can't get it to match anything.

Netizen

11:29 am on May 17, 2004 (gmt 0)

10+ Year Member



Ahh, always forget that it should be

"/some pattern/s"

when there are newlines in the string and you use the "." pattern matching character.

timster

5:48 pm on May 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



preg_match('/(<p>.*?<\/p>)[\t\n\s]*(.*)/',$this->description, $regs);

That will work, but can be a little simpler/faster, since \s matches tabs and newlines:

preg_match('/(<p>.*?<\/p>)\s*(.*)/', $this->description, $regs);