如何通过URL判断页面是否为HTML?(SIMPLE-HTML-DOM或file_get_contents?)$str = <<<EOT
www.baidu.com
www.crazycoder.cn/mypdf/46304.pdf
http://rss.eastday.com/china.xml
EOT;我需要这样的结论if not html format{
echo url;//这里仅仅返回 url 地址
}else{
echo url;//这里返回的 url 地址做下一步工作
}谢谢。

解决方案 »

  1.   

    $substr 查找字符串,.html 或者.htm等  
      

  2.   

    这个貌似有点麻烦。因为网站如果使用了框架的话,一般是不会有.htm或.html抑或.php之类的。真正要实现辨认可能还是需要模拟登录链接,获取页面内容,然后写个简单点的正则表达式匹配一下有没有<html></html>标签即可。
      

  3.   

    <?php$str = <<<EOT
    www.baidu.com
    www.crazycoder.cn/mypdf/46304.pdf
    http://rss.eastday.com/china.xml
    EOT;$ch = curl_init();$options = array(
    CURLOPT_HEADER => 0, 
    CURLOPT_FRESH_CONNECT => 1, 
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_FORBID_REUSE => 1, 
    CURLOPT_TIMEOUT => 10, 
    );
    curl_setopt_array($ch, ($options));$urls = array_map("trim", explode("\n", str_replace("\r\n", "\n", $str)));foreach($urls as $url) {

    // 设置url
    curl_setopt($ch, CURLOPT_URL, $url);

    echo '执行查询: ' . $url . ' , 结果是 :';
    // Execute
    $result = curl_exec($ch);

    // Check if any error occured
    if(!curl_errno($ch)) {
    // 获取内容的类型(也就是MIME类型,具体看 http://www.w3school.com.cn/media/media_mimeref.asp)
    $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

    // 判断是否html内容
    if(strpos($content_type, 'text/html') !== false) {
    echo '我是一个html的内容';
    } else {
    echo '我不是一个html的内容,我的内容格式是: ' . $content_type;
    }

    } else {
    echo ' 有错了, '.curl_error($ch);
    }

    echo '<br/>';
    }当然,如果你不想取不想要的结果,你可以使用fsockopen,不过那样你需要做更多的底层代码。