This article mainly introduces PHP to get the title of the Web page 3 implementation methods, respectively, using curl, file () function, file_get_contents implementation, the need for friends can refer to the following
First, the recommended method curl get <?php $c = Curl_init (); $url = ' www.jb51.net '; curl_setopt ($c, Curlopt_url, $url); curl_setopt ($c, Curlopt_returntransfer, 1); $data = curl_exec ($c); Curl_close ($c); $pos = Strpos ($data, ' utf-8 '); if ($pos ===false) {$data = Iconv ("GBK", "Utf-8", $data);} preg_match ("/<title> (. *) </title>/i", $data, $ title); echo $title [1];?> II, using the file () function <?php $lines _array = file (' http://www.jb51.net/'); $lines _string = Implode (", $lines _array); $pos = Strpos ($lines _string, ' utf-8 '); if ($pos ===false) {$lines _string = iconv ("GBK", "Utf-8", $lines _string); Eregi ("<title> (. *) </title>", $ Lines_string, $title); echo $title [1];?> III, using file_get_contents <?php $content =file_get_contents ("http://www.jb51.net/") ; $pos = Strpos ($content, ' utf-8 '); if ($pos ===false) {$content = Iconv ("GBK", "Utf-8", $content);} $postb =strpos ($content, ' <title> ') +7; $poste =strpos ($content, ' </title> '); $length = $poste-$postb; Echo substr ($conTent, $POSTB, $length);?>