This article mainly introduces three methods for PHP to obtain the webpage title, which are implemented by using the CURL, file () function, and file_get_contents function. For more information about how to obtain the webpage title, see the next and recommended CURL method.
$ C = curl_init ();
$ Url = 'www .php.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>/I ", $ data, $ title); <BR> echo $ title [1]; <BR >?> </P> <P> 2. Use the file () function </P> <? Php <BR> $ lines_array = file ('HTTP: // www.php.net/'); <BR> $ lines_string = implode ('', $ lines_array ); <BR> $ pos = strpos ($ lines_string, 'utf-8'); <BR> if ($ pos = false) {$ lines_string = iconv ("gbk ", "UTF-8", $ lines_string) ;}< BR> eregi ("<title> (. *)", $ Lines_string, $ title );
Echo $ title [1];
?>
3. Use file_get_contents
$ Content = file_get_contents ("http://www.php.net /");
$ Pos = strpos ($ content, 'utf-8 ');
If ($ pos = false) {$ content = iconv ("gbk", "UTF-8", $ content );}
$ Postb = strpos ($ content ,'') + 7; <BR> $ poste = strpos ($ content ,'');
$ Length = $ poste-$ postb;
Echo substr ($ content, $ postb, $ length );
?>