Through the URL to crawl the title of the page, some sites do not catch, method stupid, seeking guidance.
This post was last edited by u012716911 on 2013-11-04 11:25:29
The code is my own thinking of writing, I do not know there is no better way. Would you please give me some pointers?
Some sites can be caught, such as Baidu, some sites will not catch, such as the home of the Pacific automobile.
Public Function Set_title ()
{
Get in URL
$url = $_post[' url '];
$url = "www.pcauto.com.cn"; Can't catch it!
A series of curl settings
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, curlopt_header,0);
curl_setopt ($ch, curlopt_encoding, ' gzip ');
curl_setopt ($ch, curlopt_returntransfer,1);
$content _source = curl_exec ($ch);
Curl_close ($ch);
Get the encoded format of captured content
transcoding
$content _source = Iconv ($encode, ' Utf-8//ignore ', $content _source);
Intercept
if (Preg_match ("/(.*?) <\/title>/i ", $content _source, $title))
{
echo $title [1];
}
Else
{
Echo ' pull header failed ';
}
}
Curl Crawl title
Share to:
------Solution--------------------
The problem is a regular match there, you can add an S modifier.
if (Preg_match ("/ <title>(.*?) <\/title>/is ", $content _source, $title))</title>
s If this modifier is set, the dot metacharacters (.) in the pattern
matches all characters, including line breaks. Without this setting, no line break is included。