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.