欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP获取远道网站标题及简介说明

程序员文章站 2022-05-20 19:44:53
...
PHP获取远程网站标题及简介说明
本帖最后由 mingniu66 于 2013-11-20 16:32:18 编辑 header("Content-Type:text/html;charset=utf-8");
$url = 'http://cm100.net/';

//获取远程网页的编码
function _charset($url){
$text = file_get_contents($url);
$mode = '/charset=(.*)\"/iU';
preg_match($mode,$text,$result);
return $result[1];
}
$charset = _charset($url);//获取远程网页的编码
//以上为公用


//1:获取远程网页的标题,$url地址,$charset用于判断编码
function _title($url,$charset){
$text = file_get_contents($url);

//如果是gb2312编码处理乱码
if ($charset == 'gb2312'){
$text = iconv('gb2312','utf-8',$text);
}

$mode = '/(.*)/iU';<br> preg_match($mode,$text,$result);<br> return $result[1];<br> }<br> echo '网站标题:'.$title = _title($url,$charset); //获取远程网页的标题<br><br><br><br> //2:获取远程网页的简介<br> function _description($url,$charset){<br> $text = file_get_contents($url);<br><br> //gb2312编码乱码处理<br> if ($charset == 'gb2312'){<br> $text = iconv('gb2312','utf-8',$text);<br> }<br><br> $mode = '/<meta>/iU';<br> preg_match($mode,$text,$result);<br> return $result[1];<br><br> }<br> echo '网站简介:'.$description = _description($url,$charset); //获取远程网页的简介<br> ?><br><br> 初学PHP,想要获取远程网站的标题及简介,网上查了好几天写出来的结果,勉强能达到想要的结果,获取速度有点慢,感觉代码很乱,不知道有没有更好的方法, <div class="topic-extra-info"> <div class="tag"> <div class="social-share"> <span class="prompt">分享到:</span> <span class="social-share-buttons" data-title="【PHP获取远程网站标题及简介说明】<?php?header("Content-Type:text/html;charset=utf-8");$url?=?'http://cm100.net/';?//获取远程..." data-pics=""></span> <br><font color="#FF8000">------解决方案--------------------</font><br>你可以合成一个函数啊,那就应该很快了。<br><font color="#FF8000">------解决方案--------------------</font><br>你这分析一个页面要获取多少次阿得<br><font color="#FF8000">------解决方案--------------------</font><br>http://blog.csdn.net/jdgdf566/article/details/13632111<br><font color="#FF8000">------解决方案--------------------</font><br>你看你每个函数里都用file_get_contents。当然慢啦。<br> 应该只有1个函数用来file_get_contents获取内容,然后将内容传给每个函数来匹配结果! <div class="clear"> <a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2suaHRtbD90PTI=" target="_blank" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cHM6Ly9pbWcucGhwLmNuL3VwbG9hZC9jb3Vyc2UvMDAwLzAwMC8wNzEvNjIwYzYwNGY3NTI1MTk5NS5wbmc=" style="margin-top: 30px;" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a><p style="float:right;font-size:13px;color:#999;"><span class="red">声明:</span>本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。</p> </div> <p class="article-relative-header">相关文章</p> <p class="article-relative-header">相关视频</p> <hr class="layui-clear"> <ul class="article-relative-ul"> <li> <span class="layui-badge-dots"></span><a class="relevant" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg4ODkuaHRtbA==" target="_blank" rel="nofollow">详解win10下PHP的安装配置(以php5.6为...</a> </li> <li> <span class="layui-badge-dots"></span><a class="relevant" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg4MzUuaHRtbA==" target="_blank" rel="nofollow">php Swoole实现毫秒定时计划任务(详解)</a> </li> <li> <span class="layui-badge-dots"></span><a class="relevant" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg4NTQuaHRtbA==" target="_blank" rel="nofollow">【DTM】PHP协程客户端v0.1 beta版本发...</a> </li> <li> <span class="layui-badge-dots"></span><a class="relevant" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy0yOTg2MTkuaHRtbA==" target="_blank" rel="nofollow">PHP中类的理解和应用[一]_PHP教程</a> </li> <li> <span class="layui-badge-dots"></span><a class="relevant" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy0yMDk3NTcuaHRtbA==" target="_blank" rel="nofollow"> PHP获取远道网站标题及简介说明 </a> </li> </ul> <ul class="article-relative-ul"> <li> <span class="layui-badge-dots"></span><a class="relevant" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvZGUvNDY5MTguaHRtbA==" target="_blank" title="接口与抽象类01" rel="nofollow">接口与抽象类01</a> </li> <li> <span class="layui-badge-dots"></span><a class="relevant" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvZGUvNDY5MTcuaHRtbA==" target="_blank" title="类的composer自动加载机制" rel="nofollow">类的composer自动加载机制</a> </li> <li> <span class="layui-badge-dots"></span><a class="relevant" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvZGUvNDY5MTYuaHRtbA==" target="_blank" title="composer原理与常用命令03" rel="nofollow">composer原理与常用命令03</a> </li> <li> <span class="layui-badge-dots"></span><a class="relevant" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvZGUvNDY5MTUuaHRtbA==" target="_blank" title="composer原理与常用命令02" rel="nofollow">composer原理与常用命令02</a> </li> </ul> </div> <div class="comment layui-clear CopyrightData"> <div class="J_Header" id="J_Header"> <p class="header-title">网友评论</p> <p class="header-protocol">文明上网理性发言,请遵守 新闻评论服务协议</p>我要评论</div> <div class="text-box layui-clear article_comment_textarea"> <div class="box-left"> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuamF2YXNjcmlwdDpsb2dpbl90cmlnZ2VyKCk=" target="_blank" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=L3N0YXRpYy9pbWFnZXMvdXNlcl9hdmF0YXIuanBn" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuamF2YXNjcmlwdDpsb2dpbl90cmlnZ2VyKCk=" target="_blank" rel="nofollow"></a></li> </div> <div class="box-right"> <textarea name="desc" placeholder="请输入内容" class="layui-textarea article_textarea"></textarea><button class="layui-btn article_publish" data-id="209757">立即提交</button> </div> </div> </div> <div class="article-hot layui-clear" style="margin-top:0"> <h2>专题推荐</h2> <ul> <li> <a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL21hcC9kdWd1Lmh0bWw=" target="_blank" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cHM6Ly9pbWcucGhwLmNuL3VwbG9hZC9hcnRpY2xlLzAwMC8wMDAvMDAzLzVkMWVmMWU5ZTg2NmU2MzUuanBn" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a><a class="course-title" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL21hcC9kdWd1Lmh0bWw=" target="_blank" rel="nofollow">独孤九贱-php全栈开发教程</a><p class="course-type">全栈 <i class="layui-icon layui-icon-user"></i> 170W+</p> <p class="course-des">主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门</p> </li> <li> <a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL21hcC95dW52Lmh0bWw=" target="_blank" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cHM6Ly9pbWcucGhwLmNuL3VwbG9hZC9hcnRpY2xlLzAwMC8wMDAvMDAzLzVkMWVmMjM2Y2E4Nzg5NDkuanBn" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a><a class="course-title" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL21hcC95dW52Lmh0bWw=" target="_blank" rel="nofollow">玉女心经-web前端开发教程</a><p class="course-type">入门 <i class="layui-icon layui-icon-user"></i> 80W+</p> <p class="course-des">主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门</p> </li> <li> <a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3RvdXRpYW8tNDA5MjIxLmh0bWw=" target="_blank" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cHM6Ly9pbWcucGhwLmNuL3VwbG9hZC9hcnRpY2xlLzAwMC8wMDAvMDAzLzVkMWVmMjQ3N2M3ZDc1ODcuanBn" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a><a class="course-title" href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3RvdXRpYW8tNDA5MjIxLmh0bWw=" target="_blank" rel="nofollow">天龙八部-实战开发教程</a><p class="course-type">实战 <i class="layui-icon layui-icon-user"></i> 120W+</p> <p class="course-des">主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习</p> </li> </ul> </div> </div> <div class="layui-col-md4 article-list-right "> <div class="panel"> <div class="panel-title">作者信息</div> <div id="profile-2"> <div class="avatar-wrap"><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL21lbWJlci8xLmh0bWw=" target="_blank" class="avatar-link" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=L3N0YXRpYy9pbWFnZXMvdXNlcl9hdmF0YXIuanBn" class="func-um_user gravatar avatar avatar-120 um-avatar um-avatar-uploaded" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a></div> <div class="profile-info"> <p><span class="author-name"></span><i class="layui-icon layui-icon-auz" title="认证作者"></i></p> <p class="author-description">认证0级讲师</p> </div> </div> </div> <div class="layui-clear"><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuamF2YXNjcmlwdDpwdWJsaXNoX2FydGljbGUoKQ==" class="layui-btn layui-btn-danger article-add" target="_blank" rel="nofollow">发布技术文章</a></div> <div class="top-article layui-clear"><div class="layui-tab layui-tab-card"> <ul class="layui-tab-title"> <li class="layui-this">最新文章</li> <li>热门排行</li> </ul> <div class="layui-tab-content"> <div class="layui-tab-item layui-show"><ul> <p class="top-img"><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2suaHRtbD90PTI=" target="_blank" title="线上直播班" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cHM6Ly9pbWcucGhwLmNuL3VwbG9hZC9jb3Vyc2UvMDAwLzAwMC8wNzEvNjIwYzY1MTc4NTg5ZDg5NS5wbmc=" style="width: 100%" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a></p> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg4OTAuaHRtbA==" target="_blank" title="一文详解Windows和Linux环境下怎么安装配置PHP" rel="nofollow">一文详解Windows和Linux环境下怎么安装配置PHP</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg4ODkuaHRtbA==" target="_blank" title="详解win10下PHP的安装配置(以php5.6为例)" rel="nofollow">详解win10下PHP的安装配置(以php5.6为例)</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg4NTQuaHRtbA==" target="_blank" title="【DTM】PHP协程客户端v0.1 beta版本发布啦!" rel="nofollow">【DTM】PHP协程客户端v0.1 beta版本发布啦!</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg4MzcuaHRtbA==" target="_blank" title="【吐血整理】28个关于PHP核心技术的面试题,助力跳槽!" rel="nofollow">【吐血整理】28个关于PHP核心技术的面试题,助力跳槽!</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg4MzUuaHRtbA==" target="_blank" title="php Swoole实现毫秒定时计划任务(详解)" rel="nofollow">php Swoole实现毫秒定时计划任务(详解)</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg3NjguaHRtbA==" target="_blank" title="用PHP将女友照片转成可爱的动漫头像!" rel="nofollow">用PHP将女友照片转成可爱的动漫头像!</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg3MzEuaHRtbA==" target="_blank" title="PHP 文件操作函数(专题)" rel="nofollow">PHP 文件操作函数(专题)</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00ODg3MDQuaHRtbA==" target="_blank" title="PHP ARRAY 数组函数(专题)" rel="nofollow">PHP ARRAY 数组函数(专题)</a></li> </ul></div> <div class="layui-tab-item"><ul> <p class="top-img"><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3RvdXRpYW8tMzg3MDcyLmh0bWw=" target="_blank" title="面对巨额亏损的PHP中文网,我该为情怀买单吗?(猪哥)" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cHM6Ly9pbWcucGhwLmNuL3VwbG9hZC9hcnRpY2xlLzAwMC8wMDAvMDAzLzViODkwYTllNDQxN2Y4NzIucG5n" style="width: 100%" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a></p> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy0yNTU0NTguaHRtbA==" target="_blank" title="循环大量数据导致内存超出,不增加内存如何解决该问题" rel="nofollow">循环大量数据导致内存超出,不增加内存如何解决该问题</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00MTM1MzguaHRtbA==" target="_blank" title="VPN是什么" rel="nofollow">VPN是什么</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy0zOTM3MDAuaHRtbA==" target="_blank" title="最全最详细的PHP面试题(带有答案)" rel="nofollow">最全最详细的PHP面试题(带有答案)</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy0zODY3MDkuaHRtbA==" target="_blank" title="PHP四大主流框架的优缺点总结" rel="nofollow">PHP四大主流框架的优缺点总结</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00NDgzOTEuaHRtbA==" target="_blank" title="PHP快速搭建一个简单的QQ机器人" rel="nofollow">PHP快速搭建一个简单的QQ机器人</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00NDgyODYuaHRtbA==" target="_blank" title="API常用签名验证方法(PHP实现)" rel="nofollow">API常用签名验证方法(PHP实现)</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00NDgzMDkuaHRtbA==" target="_blank" title="PHP常用日期时间操作合集" rel="nofollow">PHP常用日期时间操作合集</a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL3BocC13ZWl6aWppYW9jaGVuZy00NDgzMTAuaHRtbA==" target="_blank" title="Mac电脑设置hosts的方法(图文步骤)" rel="nofollow">Mac电脑设置hosts的方法(图文步骤)</a></li> </ul></div> </div> </div></div> <div class="article-ad article-course php-news-right-imginfo"> <div class="r-title"> <i class="layui-icon layui-icon-praise"></i>推荐视频教程</div> <li> <a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS8xNzkuaHRtbA==" target="_blank" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cHM6Ly9pbWcucGhwLmNuL3VwbG9hZC9jb3Vyc2UvMDAwLzAwMC8wMDMvNWExNzk4MmQ0ODY4NjI1NC5qcGc=" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a><span><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS8xNzkuaHRtbA==" target="_blank" rel="nofollow">javascript初级视频教程</a></span> </li> <li> <a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS8xODEuaHRtbA==" target="_blank" rel="nofollow"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cHM6Ly9pbWcucGhwLmNuL3VwbG9hZC9jb3Vyc2UvMDAwLzAwMC8wMDMvNWExNzkyODNjMzg4NTcwMy5qcGc=" alt="PHP获取远道网站标题及简介说明" title="PHP获取远道网站标题及简介说明"></a><span><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS8xODEuaHRtbA==" target="_blank" rel="nofollow">jquery 基础视频教程</a></span> </li> </div> <div class="article-ad mt-20" id="aritcle_detail_new"> <div class="r-title"> <i class="layui-icon layui-icon-video"></i>视频教程分类</div> <ul class="course-category-lists layui-clear"> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS9saXN0LzI5L3R5cGUvMi5odG1s" target="_blank" rel="nofollow"><div class="course-category-child-first">php视频教程</div></a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS9saXN0LzExLmh0bWw=" target="_blank" rel="nofollow"><div class="course-category-child-first">html视频教程</div></a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS9saXN0LzEyLmh0bWw=" target="_blank" rel="nofollow"><div class="course-category-child-first">css视频教程</div></a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS9saXN0LzE3Lmh0bWw=" target="_blank" rel="nofollow"><div class="course-category-child-first">JS视频教程</div></a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS9saXN0LzE5Lmh0bWw=" target="_blank" rel="nofollow"><div class="course-category-child-first">jQuery视频教程</div></a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS9saXN0LzUxLmh0bWw=" target="_blank" rel="nofollow"><div class="course-category-child-first">mysql视频教程</div></a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS9saXN0LzMzLmh0bWw=" target="_blank" rel="nofollow"><div class="course-category-child-first">Linux视频教程</div></a></li> <li><a href="/default/index/url?u=aHR0cHM6Ly93d3cucGhwLmNuL2NvdXJzZS9saXN0LzMwLmh0bWw=" target="_blank" rel="nofollow"><div class="course-category-child-first">Python视频教程</div></a></li> </ul> </div> </div> </div>