Re: What's wrong with my regular expression?
| From: | Marco Tabini | Date: | Mon, 14 Oct 2002 12:08:57 +0000 |
| Subject: | Re: What's wrong with my regular expression? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-119870@lists.php.net to get a copy of this message | ||
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
>