Re: Newb Question - find and record

From: Date: Thu, 09 May 2002 06:46:10 +0000
Subject: Re: Newb Question - find and record
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-96774@lists.php.net to get a copy of this message
On Thursday 09 May 2002 06:34, David Duong wrote: > I know the first part. (I thought maybe their was a function that can be > used in conjunction with matching.) > > This is what I am looking at. > > preg_match_all ("|<[^>]+>(.*)</[^>]+>|U", > "<b>example: </b><div align=left>this is a test</div>", > $out, PREG_PATTERN_ORDER); > print $out[0][0].", ".$out[0][1]."\n"; > print $out[1][0].", ".$out[1][1]."\n" > > The only part that confuses me is the [^>]. How would you have it search > only for Test tags?. > > This: > preg_match_all ("|<test+>(.*)</test+>|U", > "<b>example: </b><test>this is a test</test>", > $out, PREG_PATTERN_ORDER); > print $out[0][0].", ".$out[0][1]."\n"; > print $out[1][0].", ".$out[1][1]."\n" > > prints out: > > Warning: Undefined offset: 1 in > C:\WINDOWS\DESKTOP\DAVID'~1\PROJECTS\SNR\phpF071.PHP > on line 7 > this is a test, > Warning: Undefined offset: 1 in > C:\WINDOWS\DESKTOP\DAVID'~1\PROJECTS\SNR\phpF071.PHP > on line 8 > this is a test, > > What is the error? Your expression probably didn't match anything. Try: preg_match_all("|<test>(.*)</test>|U", ... However I believe this is greedy and will match /everything/ from the first <test> tag to the last </test> tag. I'll leave it as an exercise for you to modify it so it's non-greedy. Hint, the examples in the manual (I think there was one about stripping HTML tags) gives a good starting point. Also for debugging, a better way to display the contents of $out is: print_r($out); -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* A computer lets you make more mistakes faster than any other invention, with the possible exceptions of handguns and Tequilla. -- Mitch Ratcliffe */

« previous php.general (#96774) next »