|
// $ Html = the html on the page
// $ Current_url = the full url that the html came from
// (Only needed for $ repath)
// $ Repath = converts ../and // urls to full valid urls
Function pageLinks ($ html, $ current_url = "", $ repath = false ){
Preg_match_all ("/\
$ Links = array ();
If (isset ($ matches [2]) {
$ Links = $ matches [2];
}
If ($ repath & count ($ links)> 0 & strlen ($ current_url)> 0 ){
$ Pathi = pathinfo ($ current_url );
$ Dir = $ pathi ["dirname"];
$ Base = parse_url ($ current_url );
$ Split_path = explode ("/", $ dir );
$ Url = "";
Foreach ($ links as $ k => $ link ){
If (preg_match ("/^ \. \./", $ link )){
$ Total = substr_count ($ link ,"../");
For ($ I = 0; $ I <$ total; $ I ++ ){
Array_pop ($ split_path );
}
$ Url = implode ("/", $ split_path). "/". str_replace ("../", "", $ link );
} Elseif (preg_match ("/^ \/", $ link )){
$ Url = $ base ["scheme"]. ":". $ link;
} Elseif (preg_match ("/^ \/| ^. \ //", $ link )){
$ Url = $ base ["scheme"]. ": //". $ base ["host"]. $ link;
} Elseif (preg_match ("/^ [a-zA-Z0-9]/", $ link )){
If (preg_match ("/^ http/", $ link )){
$ Url = $ link;
} Else {
$ Url = $ dir. "/". $ link;
}
}
$ Links [$ k] = $ url;
}
}
Return $ links;
}
Header ("content-type: text/plain ");
$ Url = "http://www.jb51.net ";
$ Html = file_get_contents ($ url );
// Gets links from the page:
Print_r (pageLinks ($ html ));
// Gets links from the page and formats them to a full valid url:
Print_r (pageLinks ($ html, $ url, true ));
|