Re: [PHP] Returning mutliple matches of a regex with preg_match()
Also, make sure that you make the match ungreedy with the U modifier, or use: (.*?) James Niklas lampén wrote in message 000501c1d0aa$4a5d97f0$ba93c5c3@Niklas">news:000501c1d0aa$4a5d97f0$ba93c5c3@Niklas... preg_match_all(); Niklas -Original Message- From: Stefen Lars [mailto:[EMAIL PROTECTED]] Sent: 21. maaliskuuta 2002 7:28 To: [EMAIL PROTECTED] 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: -- BLOCKQUOTEP Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines /P/BLOCKQUOTE pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP Pig, Pig, Pig, Pig, Pig /P/BLOCKQUOTE -- I want to return all the stuff between BLOCKQUOTEP ... /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(/BLOCKQUOTEP(.*)\/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) BLOCKQUOTEP Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines /P/BLOCKQUOTE pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP 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 pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Returning mutliple matches of a regex with preg_match()
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(/BLOCKQUOTEP(.*)\/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 [EMAIL PROTECTED] Reply-To: liljim [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: [PHP] Returning mutliple matches of a regex with preg_match() Date: Thu, 21 Mar 2002 11:42:35 - Also, make sure that you make the match ungreedy with the U modifier, or use: (.*?) James Niklas lampén wrote in message 000501c1d0aa$4a5d97f0$ba93c5c3@Niklas">news:000501c1d0aa$4a5d97f0$ba93c5c3@Niklas... preg_match_all(); Niklas -Original Message- From: Stefen Lars [mailto:[EMAIL PROTECTED]] Sent: 21. maaliskuuta 2002 7:28 To: [EMAIL PROTECTED] 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: -- BLOCKQUOTEP Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines /P/BLOCKQUOTE pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP Pig, Pig, Pig, Pig, Pig /P/BLOCKQUOTE -- I want to return all the stuff between BLOCKQUOTEP ... /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(/BLOCKQUOTEP(.*)\/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) BLOCKQUOTEP Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines /P/BLOCKQUOTE pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP 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 pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP 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 worlds largest e-mail service with MSN Hotmail. http://www.hotmail.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Returning mutliple matches of a regex with preg_match()
preg_match_all(); Niklas -Original Message- From: Stefen Lars [mailto:[EMAIL PROTECTED]] Sent: 21. maaliskuuta 2002 7:28 To: [EMAIL PROTECTED] 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: -- BLOCKQUOTEP Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines /P/BLOCKQUOTE pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP Pig, Pig, Pig, Pig, Pig /P/BLOCKQUOTE -- I want to return all the stuff between BLOCKQUOTEP ... /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(/BLOCKQUOTEP(.*)\/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) BLOCKQUOTEP Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines /P/BLOCKQUOTE pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP 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 pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Returning mutliple matches of a regex with preg_match()
use preg_match_all(); the var_dump($matches); to see what happened. Best regards, Andrey Hristov - Original Message - From: Stefen Lars [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, March 21, 2002 7:27 AM 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: -- BLOCKQUOTEP Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines /P/BLOCKQUOTE pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP Pig, Pig, Pig, Pig, Pig /P/BLOCKQUOTE -- I want to return all the stuff between BLOCKQUOTEP ... /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(/BLOCKQUOTEP(.*)\/P\/BLOCKQUOTE/i,$content,$matches)) { echo pre; var_dump($matches); echo /pre; } ? -- For the above, var_dump() returns this: -- array(2) { [0]= string(556) BLOCKQUOTEP Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow Cow, Cow, Cow, Cow, Cow a lot of lines /P/BLOCKQUOTE pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP 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 pboring stuff - we are not interested in this/p BLOCKQUOTEP Chicken, Chicken, Chicken Chicken, Chicken, Chicken Chicken, Chicken, Chicken more lines /P/BLOCKQUOTE pmore boring stuff - we are not interested in this/p BLOCKQUOTEP Rabbit, Rabbit, Rabbit, Rabbit /P/BLOCKQUOTE peven more boring stuff - we are not interested in this/p BLOCKQUOTEP 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php