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;