PHP remote access to Web content

Source: Internet
Author: User
1. Intercepting a PHP class file mainly involves getting the HTML content of the URL and then making it from the HTML content Regular ExpressionsThe
Include ' stringbuilder.php '; class Cutpage{function __construct () {}//method One: Connection gets the text content of the whole file function getallcontent ($url) { $resouce =fopen ($url, "R") or Die ("File open failed!") if (! $resouce) {echo "Request file does not exist!) ";} $ALLC//length is too short to be used for stringbuilder$sb=new StringBuilder (), while (!feof ($resouce)) {//if not to the end of the file $line= Fgets ($resouce, 4096); $SB->append ($line);} Fclose ($resouce); return $SB->tostring ();} Method Two: Get all the text to match the text title---------format: xxxx (xx) XXX form expression function matchcontenttitle ($content, $regex _title) {//echo "Regex:". $regex _title;if (Preg_match ($regex _title, $content)) {$array =preg_split ($regex _title, $content, -1,preg_ split_delim_capture); return  $array [1];} Else{echo "Match failed! ";}} The matching section returns the ArrayFunction Matchcontentchapter ($content, $regex _chapter) {if (Preg_match_all ($regex _chapter, $) that carry the chapter. Content, $matcher)) {return $matcher [1];} Else{echo "Match failed! "; return;}}}

Test PHP files

Include ' cutpage.php '; class Test{publicstatic function  start ($url, $regex _title, $regex _chapter) {$cut =new Cutpage (); $all = $cut->getallcontent ($url); $returnTitle = $cut->matchcontenttitle ($all, $regex _title); echo " Title: ". $returnTitle; $arrays = $cut->matchcontentchapter ($all, $regex _chapter); for ($i =0; $i
 
   
  Regular Expressions $regex_title= "/\s*[<]div id=\" title\ ">[<][h][1]> (\s+) [<]\/[h][1]>[<]\/div>\s*/]; $ regex_chapter= "/\s*[<]a href=\" \d{7}[. Html\ "[\w+] [<]\/a>\s*/";//chapter Test::start ($url, $regex _title, $regex _chapter);//echo "cut". $return;// The
  Regular expression
 of matching Chinese characters ^[\u4e00-\u9fa5]+

This builds a StringBuilder class similar to Java
Class stringbuilder{    const line= "
"; Protected $list = Array ("); Public function __construct ($str =null) { Array_push ($this->list, $str); } Public function append ($str) { Array_push ($this->list, $str); return $this; } Public Function Appendline ($str) { Array_push ($this->list, $str. self::line); return $this; } Public Function AppendFormat ($str, mixed $args) { Array_push ($this->list, sprintf ($str, $args)); return $this; } Public Function toString () { return implode ("", $this->list); } Public Function __destruct () { unset ($this->list);} }

The above describes the PHP remote access to Web content, including the regular expression of content, I hope the PHP tutorial interested in a friend to help.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.