javascript-php automatically generates a link to the URL and then extracts the title

Source: Internet
Author: User
If you enter a URL that automatically generates a link, it will fetch the title display of the link after that. Is there any good way to do it?
Either PHP or JS is ok.

Reply content:

If you enter a URL that automatically generates a link, it will fetch the title display of the link after that. Is there any good way to do it?
Either PHP or JS is ok.

1 user-entered URLs are asynchronously submitted to PHP after a simple URL legality check with jquery in the front end
2 php detection URL is legitimate, use curl to get the content of the URL
3 PHP Gets the title or error message back to the front-end jquery.

!--? php//file encoded as UTF-8 (no BOM) error_reporting (E_all | | E_STRICT); $url = ' http://q i q.com ';//First determine if the URL entered by the user is valid if (!filter_var ($url, Filter_validate_url)) {die (' You are not entering a normal URL. ');} $ch = Curl_init (); Curl_setopt_array ($ch, Array (Curlopt_header =--> false, Curlopt_url = $url,//Considering that some sites are 301 hops Turn the. Curlopt_followlocation = True, the//connection timeout time is set to 5 seconds Curlopt_connecttimeout = 5,//Response timeout is 5 seconds curlopt_timeout = 5, Curlopt_verbose = False, Curlopt_autoreferer = True,//Receive all codes curlopt_encoding = ", Return page Content Curlopt_returntransfer = true,)); $response = Curl_exec ($ch);//Detection page encoding, the non-UTF-8 encoded pages, unified conversion to UTF-8 processing. if (' UTF-8 '!== ($encoding = mb_detect_encoding ($response, Array (' UTF-8 ', ' CP936 ', ' ASCII ')))) {$response = Mb_convert_enco Ding ($response, ' UTF-8 ', $encoding);} Match Title$title = ' failed to get to title '; if (Preg_match (' #  (. *)  #isU ', $response, $match)) {$title = $match [1];} echo $title; 
  • Related Article

    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.