How to get only the text content of the article in php

藏色散人
Release: 2023-03-17 15:16:01
Original
5999 people have browsed it

php method to get only the text content of the article: 1. Create a PHP sample file; 2. By defining "function curl_request ( $url , $post = '' , $cookie = '' , $returnCookie = 0 ) {...}" method can only capture the text content of the web page and filter its tags.

How to get only the text content of the article in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to get only the text content of the article in php?

php only grabs the text content of the web page body, and filters the web page tags

php only grabs the text content of the web page, and filters its tags, just do it, start!

The code is as follows:

<?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 );
?>
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to get only the text content of the article in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template