Re: What's wrong with my regular expression?

From: Date: Mon, 14 Oct 2002 12:18:06 +0000
Subject: Re: What's wrong with my regular expression?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-119874@lists.php.net to get a copy of this message
Yep, but the problem is that ereg doesn't return more than one match, so you've got to use preg_match_all (I think). The snippet I sent you should work (at least, it does on my system). On Mon, 2002-10-14 at 08:22, Ns_Andy wrote: > Since I don't know perl,I use ereg instead. > > but ,thanks. > > -- > Regards, > > "Marco Tabini" <marcot@tabini.ca> wrote in message > news:1034597339.9026.832.camel@localhost.localdomain... > > If you want to match more than one occurrence of the same pattern, you > > must use preg_match_all with Perl syntax. The matches will be returned > > as an array of arrays (that is, an array that contains the result of a > > normal ereg call for each match found): > > > > $line = '<a > > href="http://www.yahoo.com">http://www.yahoo.com</a>'; > > $line .= 'X<a > > href=http:/www.php.net>www.php.net</a>'; > > if (preg_match_all("/(<( )*[Aa]( )*[Hh][Rr][Ee][Ff]( )*=( > > )*[\"'][^<>\"']*[\"']( )*>[^<>]*<( )*\/( > > )*a( )*>)/",$line,$matches)) > > { > > print ($matches[0][0]); // Returns the first match > > print ($matches[1][0]); // Returns the second match > > } > > > > Also, your regular expression didn't cover all the possibility (eg: > > spaces between href and =. I might have overdone it with mine but, hey, > > I've got a turkey to cook and it's already late ;-) > > > > Cheers, > > > > > > Marco > > > > On Mon, 2002-10-14 at 07:40, Ns_Andy wrote: > > > $line = '<a > > > href="http://www.yahoo.com">http://www.yahoo.com</a>'; > > > if (eregi("<a[ ]+(.*[ > > > ]+)?href=([\"'])?([^<>\"'])+([\"'])?>[^<>]+<\/a[ > > > ]*>",$line,$matches)) { > > > print (htmlspecialchars($matches[0])); > > > } > > > > > > it works fine, > > > however if > > > $line .= 'X<a > > > href=http:/www.php.net>www.php.net</a>'; > > > is added before eregi, > > > it doesn't,what's wrong with my code? > > > -- > > > Regards, > > > > > > > > > > > > -- > > > 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 >

« previous php.general (#119874) next »