RE: [PHP] Extracting urls from HTML
| From: | WILSON, Nick | Date: | Thu, 26 Oct 2000 06:43:26 +0000 |
| Subject: | RE: [PHP] Extracting urls from HTML | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-22070@lists.php.net to get a copy of this message | ||
preg_match_all("'\s+href\s*=\s*([\"\'])? (?(1) (.*?)\\1 | ([^\s\>]+))
'isx",$document,$links);
This will match the link part of a href, if the href is between " or ' then
it will match everything between the quotes. If there are no quotes, then
it will match everything up until the next space or > symbol.
Some links may be relative though so may need expanding out ...
As for the text, something like
preg_match_all("|<a\s+href.*?>(.*)?</a>|isx", $document, $links);
Hope this helps,
Nick
http://www.webclient.com.au
Get round blocking software.
> -----Original Message-----
> From: Jay Greenspan [mailto:jay@trans-city.com]
> Sent: Thursday, 26 October 2000 12:58
> To: php-general@lists.php.net
> Subject: Re: [PHP] Extracting urls from HTML
>
>
> I think I'd use preg_match_all(). You can see the manual
> for how it breaks up the array, which is pretty clost to what you're
> looking for.
>
> Something like this regular expression should work, I think.
> You might have to make changes depending on how you
> delimit your hrefs.
>
> preg_match_all('/<a href="([^"]*)".*>(.*)<\/a>/i', $str,
> $array);
>
>
>
> "Srinivasan Ramakrishnan" wrote in message
> <006d01c03edd$ef752420$ce87c5cb@srinivarmd3>...
> >Hi,
> >
> >I need to extract the list of links from a html file.
> >
> >Ideally I will get them into an associative array where the name part
> equals
> >the text between <a href...> and </a>, and the value part
> contains the
> >actual URL.
> >
> >I've been looking at the available string functions, and I
> can't seem to
> >find a way. Is there a class or library that does this?
> >
> >I'm too lazy to figure out how to write a parser for this,
> atleast what I'm
> >trying to do doesn't warrant that kind of effort. I have a
> pretty large
> >bookmark file, and I want to store just the links and names
> in a text file.
> >
> >Thanks,
> >
> >Srini
> >
> >
> >--
> >PHP General Mailing List (http://www.php.net/)
> >To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> >For additional commands, e-mail: php-general-help@lists.php.net
> >To contact the list administrators, e-mail:
> php-list-admin@lists.php.net
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail:
> php-list-admin@lists.php.net
>
>