web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

php如何获取文章内容(过滤html标签)

384 ℃
     

php如何获取文章body中的内容,但需要过滤html标签。下面web建站小编给大家详细介绍一下具体代码!

具体代码如下:

<?php
 function curl_request ( $url , $post = '' , $cookie = '' ,  $returnCookie = 0 ) {
   $ua = $ua==''?$_SERVER ['HTTP_USER_AGENT']:'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)' ;
      $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 [ 'cookie' ]   =  substr ( $matches [ 1 ] [ 0 ] ,  1 ) ;
         $info [ 'content' ]  =  $body ;
         return  $info ;
      } else {
         //return  $data ;
         $data=mb_convert_encoding($data, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
         preg_match("/<body.*?>(.*?)<\/body>/is",$data,$match);
         $str= trim($match[1]);
         $html = strip_tags($str);
     ​    $html_len = mb_strlen($html,'UTF-8');
 ​ ​ ​ ​ ​ ​ ​ ​ $html = mb_substr($html, 0, strlen($html), 'UTF-8');
 ​        ​$search = array(" "," ","\n","\r","\t");
         $replace = array("","","","","");
         echo str_replace($search, $replace, $html);
    }
}
curl_request ( $url, $post = '' , $cookie = '' ,  $returnCookie = 0 );
?>

php建站中的错误级别有哪些?

用php搭建的网站一般会有哪些安全漏洞

php导出mysql csv出现乱码是什么原因(附解决方法)

标签: php建站

上面是“php如何获取文章内容(过滤html标签)”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_3361.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > php入门
本文共计1619个字,预计阅读时长11分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款免费可商用英文字体——Atkinson Hyperlegible
下一篇: 推荐一款免费好看的中文设计字体——字体传奇特战体
x 打工人ai神器