PHP captures the webpage content and obtains the absolute path of the link and the absolute path of the image.

Source: Internet
Author: User

Webpage content Capturing Method:

 
$ Ch= @ Curl_init ($ URL); @ Curl_setopt ($ Ch, Curlopt_returntransfer,True);$ Text= @ Curl_exec ($ Ch); @ Curl_close ($ Ch);$ Text= Relative_to_absolute ($ Text,$ URL);

Relative Path to absolute path:

 Function Relative_to_absolute ( $ Content , $ Feed_url ){  Preg_match ('/(HTTP | HTTPS | FTP ):\/\//', $ Feed_url , $ Protocol  );  $ Server_url = Preg_replace ("/(HTTP | HTTPS | FTP | News ):\/\//","", $ Feed_url  );  $ Server_url = Preg_replace ("/\/.*/","", $ Server_url  ); If ( $ Server_url ='' ){  Return   $ Content  ;}  If ( Isset ( $ Protocol [0 ]) {  $ New_content = Preg_replace ('/Href = "\ //', 'href = "'. $ Protocol [0]. $ Server_url .'/', $ Content  );  $ New_content = Preg_replace ('/Src = "\ //', 'src = "'. $ Protocol [0]. $ Server_url .'/', $ New_content  );}  Else  {  $ New_content = $ Content  ;}  Return  $ New_content  ;} 

To obtain all hyperlinks:

 Function Get_links ( $ Content  ){  $ Pattern = '/<(.*?) Href = "(.*?) "(. *?)> (.*?) <\/A>/I' ;  Preg_match_all ( $ Pattern , $ Content , $ M  );  $ Re =Array_unique ( $ M [2 ]);  $ I = 0 ;  Foreach ( $ Re   As   $ Key => $ Value  ){  $ RegEx = "(HTTP | HTTPS | FTP | Telnet | News )" ;  If ((!Empty ( $ Value ) | Strlen ( $ Value )> 0 )&& Preg_match ( $ RegEx , $ Value  ))  $ Output [ $ I ++] = $ Value  ;}  Return    $ Output ;} 

How to get all image links:

 
FunctionGet_pic ($ Str){$ IMGs=Array();Preg_match_all("/(HTTP | HTTPS | FTP | Telnet | News): \/[a-z0-9 \/\-_ + = .~! % @? # % &;: $ \ () |] + \. (JPG | GIF | PNG | BMP | SWF | RAR | zip)/ISU ",$ Str,$ IMGs);Return Array_unique($ IMGs[0]);}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.