首页 > 后端开发 > PHP问题 > php怎么只获取文章文字内容

php怎么只获取文章文字内容

藏色散人
发布: 2023-03-17 15:16:01
原创
6052 人浏览过

php只获取文章文字内容的方法:1、创建一个PHP示例文件;2、通过定义“function curl_request ( $url , $post = '' , $cookie = '' ,  $returnCookie = 0 ) {...}”方法实现只抓取网页文字内容,并过滤其标签即可。

php怎么只获取文章文字内容

本教程操作环境:Windows7系统、PHP8.1版、Dell G3电脑。

php怎么只获取文章文字内容?

php只抓取网页body文字内容,并过滤网页标签

php只抓取网页文字内容,并过滤其标签,说干就干,开始!

代码如下:

<?php
 function curl_request ( $url , $post = &#39;&#39; , $cookie = &#39;&#39; ,  $returnCookie = 0 ) {
     $ua = $ua==&#39;&#39;?$_SERVER [&#39;HTTP_USER_AGENT&#39;]:&#39;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)&#39; ;
            $curl  =  curl_init ( ) ;
            curl_setopt ( $curl , CURLOPT_URL ,  $url ) ;
            curl_setopt ( $curl , CURLOPT_USERAGENT , $ua ) ;
            curl_setopt ( $curl , CURLOPT_FOLLOWLOCATION ,  1 ) ;
            curl_setopt ( $curl , CURLOPT_AUTOREFERER ,  1 ) ;
            curl_setopt ( $curl , CURLOPT_REFERER ,  "https://www.baidu.com" ) ;
            if ( $post )  {
                 curl_setopt ( $curl , CURLOPT_POST ,  1 ) ;
                 curl_setopt ( $curl , CURLOPT_POSTFIELDS ,  http_build_query ( $post ) ) ;
            }
            if ( $cookie )  {
                 curl_setopt ( $curl , CURLOPT_COOKIE ,  $cookie ) ;
            }
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt ( $curl , CURLOPT_HEADER ,  $returnCookie ) ;
            curl_setopt ( $curl , CURLOPT_TIMEOUT ,  10 ) ;
            curl_setopt ( $curl , CURLOPT_RETURNTRANSFER ,  1 ) ;
            $data  =  curl_exec ( $curl ) ;
            if  ( curl_errno ( $curl ) )  {
                 return  curl_error ( $curl ) ;
            }
            curl_close ( $curl ) ;
            if ( $returnCookie ) {
                 list ( $header ,  $body )  =  explode ( "\r\n\r\n" ,  $data ,  2 ) ;
                 preg_match_all ( "/Set\-Cookie:([^;]*);/" ,  $header ,  $matches ) ;
                 $info [ &#39;cookie&#39; ]   =  substr ( $matches [ 1 ] [ 0 ] ,  1 ) ;
                 $info [ &#39;content&#39; ]  =  $body ;
                 return  $info ;
            } else {
                 //return  $data ;
                 $data=mb_convert_encoding($data, &#39;UTF-8&#39;, &#39;UTF-8,GBK,GB2312,BIG5&#39;);
                preg_match("/<body.*?>(.*?)<\/body>/is",$data,$match);
                $str= trim($match[1]);
      $html = strip_tags($str);
    $html_len = mb_strlen($html,&#39;UTF-8&#39;);
    $html = mb_substr($html, 0, strlen($html), &#39;UTF-8&#39;);
    $search = array(" "," ","\n","\r","\t");
    $replace = array("","","","","");
    echo str_replace($search, $replace, $html);
            }
}
curl_request ( $url, $post = &#39;&#39; , $cookie = &#39;&#39; ,  $returnCookie = 0 );
?>
登录后复制

推荐学习:《PHP视频教程

以上是php怎么只获取文章文字内容的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板