Re: Returning mutliple matches of a regex with preg_match()

From: Date: Thu, 21 Mar 2002 15:20:13 +0000
Subject: Re: Returning mutliple matches of a regex with preg_match()
Groups: php.general 
Request: Send a blank email to php-general+get-89510@lists.php.net to get a copy of this message
Thank you James and Niklas. Here is the code that I need: <?PHP // open file $fd = fopen ("./text.htm", "r"); // load contents into a variable while (!feof ($fd)) { $content .= fgets($fd, 4096); } // close file fclose ($fd); // remove char returns and co $content = preg_replace("/(\r\n)|(\n\r)|(\n|\r)/", " ",$content); if (preg_match_all("/<BLOCKQUOTE><P>(.*)<\/P><\/BLOCKQUOTE>/U",$content,$matches)) { echo "<pre>"; var_dump($matches); echo "</pre>"; } ?> This returns exactly what I want (see my original post to learn what that is.) Thank again. :-)) S.
From: "liljim" <liljim@liljim.co.uk> Reply-To: "liljim" <liljim@liljim.co.uk> To: <stefenlars@hotmail.com> Subject: Re: [PHP] Returning mutliple matches of a regex with preg_match() Date: Thu, 21 Mar 2002 11:42:35 -0000 Also, make sure that you make the match "ungreedy" with the U modifier, or use: (.*?) James "Niklas lampén" wrote in message news:000501c1d0aa$4a5d97f0$ba93c5c3@Niklas... preg_match_all(); Niklas -----Original Message----- From: Stefen Lars [mailto:stefenlars@hotmail.com] Sent: 21. maaliskuuta 2002 7:28 To: php-general@lists.php.net Subject: [PHP] Returning mutliple matches of a regex with preg_match() Hello all I have been scratching my head for the last two days about this regular expression problem. I would be really VERY happy if someone could help me! I have the following text in the file 'text.htm', for example: -- <BLOCKQUOTE><P> Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines </P></BLOCKQUOTE> <p>boring stuff - we are not interested in this....</p> <BLOCKQUOTE><P> Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines </P></BLOCKQUOTE> <p>more boring stuff - we are not interested in this....</p> <BLOCKQUOTE><P> Rabbit, Rabbit, Rabbit, Rabbit </P></BLOCKQUOTE> <p>even more boring stuff - we are not interested in this....</p> <BLOCKQUOTE><P> Pig, Pig, Pig, Pig, Pig </P></BLOCKQUOTE> -- I want to return all the stuff between <BLOCKQUOTE><P> ... </P></BLOCKQUOTE> in an array. One element per match. For example, for the above text, I would like to get back an array back like this: array( "Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines", "Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines", "Rabbit, Rabbit, Rabbit, Rabbit", "Pig, Pig, Pig, Pig, Pig" ) I have been trying to do this with (many variations of) the following code: -- <?PHP // open file $fd = fopen ("./text.htm", "r"); // load contents into a variable while (!feof ($fd)) {
    $content .= fgets($fd, 4096);
} // close file fclose ($fd); // remove char returns and co. $content = preg_replace("/(\r\n)|(\n\r)|(\n|\r)/", " ",$content); // match agains regex -- this does not work correctly.... if (preg_match("/<BLOCKQUOTE><P>(.*)<\/P><\/BLOCKQUOTE>/i",$content,$matche s)) { echo "<pre>"; var_dump($matches); echo "</pre>"; } ?> -- For the above, var_dump() returns this: -- array(2) { [0]=> string(556) "<BLOCKQUOTE><P> Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines </P></BLOCKQUOTE> <p>boring stuff - we are not interested in this....</p> <BLOCKQUOTE><P> Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines </P></BLOCKQUOTE> <p>more boring stuff - we are not interested in this....</p> <BLOCKQUOTE><P> Rabbit, Rabbit, Rabbit, Rabbit </P></BLOCKQUOTE> <p>even more boring stuff - we are not interested in this....</p> <BLOCKQUOTE><P> Pig, Pig, Pig, Pig, Pig </P></BLOCKQUOTE>" [1]=> string(524) " Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines </P></BLOCKQUOTE> <p>boring stuff - we are not interested in this....</p> <BLOCKQUOTE><P> Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines </P></BLOCKQUOTE> <p>more boring stuff - we are not interested in this....</p> <BLOCKQUOTE><P> Rabbit, Rabbit, Rabbit, Rabbit </P></BLOCKQUOTE> <p>even more boring stuff - we are not interested in this....</p> <BLOCKQUOTE><P> Pig, Pig, Pig, Pig, Pig " } -- Clearly not what I want. Is my approach here incorrect? Or is it indeed possible to construct a regex to do what I want (with just one pass of the text)? Thank you in advance. :-)) S. _________________________________________________________________ Send and receive Hotmail on your mobile device: http://mobile.msn.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
_________________________________________________________________ Join the world’s largest e-mail service with MSN Hotmail. http://www.hotmail.com

« previous php.general (#89510) next »