PHP는 원격으로 웹 콘텐츠를 얻습니다.
Jul 29, 2016 am 09:11 AM
function
quot
regex
return
title
1. PHP 파일을 가로채는 작업에는 주로 URL의 HTML 콘텐츠를 가져온 다음 URL에서 정규식일치
include 'StringBuilder.php'; class CutPage{ function __construct(){ } //方法一:连接 获取真个文件的文本内容 function getAllContent($url){ $resouce=fopen($url, "r") or die("文件打开失败!"); if(!$resouce){ echo "请求文件不存在!"; } //$allc//长度太短了取不全用自购建的stringbuilder $sb=new StringBuilder(); while(!feof($resouce)){ //如果没有到文件的结尾则继续向下执行 $line=fgets($resouce,4096); $sb->append($line); } fclose($resouce); return $sb->toString(); } //方法二:获取所有的文本进行文本title的匹配---------格式为:xxxx(xx)xxx的形式表达式 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 "匹配失败!"; } } //匹配章节 返回携带章节的array function matchContentChapter($content,$regex_chapter){ if(preg_match_all($regex_chapter, $content,$matcher)){ return $matcher[1]; }else{ echo "匹配失败!"; return ; } } }
로그인 후 복사
php 파일 테스트
include 'CutPage.php'; class Test{ public static function start($url,$regex_title,$regex_chapter){ $cut=new CutPage(); $all=$cut->getAllContent($url); $returnTitle=$cut->matchContentTitle($all, $regex_title); echo "题目:".$returnTitle; $arrays=$cut->matchContentChapter($all, $regex_chapter); for($i=0;$i<count($arrays);$i++){ echo $arrays[$i]; } } } $url="http://www.quanben.com/xiaoshuo/12/12816/"; //<strong>正则表达式</strong> $regex_title="/\s*[<]div id=\"title\">[<][h][1]>(\S+)[<]\/[h][1]>[<]\/div>\s*/"; $regex_chapter="/\s*[<]a href=\"\d{7}[.]html\"[>](\W+)[<]\/a>\s*/";//章 Test::start($url, $regex_title, $regex_chapter); //echo "cut".$return; //匹配汉字的<strong>正则表达式</strong>^[\u4E00-\u9FA5]+
로그인 후 복사
class StringBuilder { const LINE="<br/>"; 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); } }
로그인 후 복사
위 내용은 정규식을 포함한 웹페이지 콘텐츠를 원격으로 얻기 위한 PHP를 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

인기 기사
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD

인기 기사
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD

뜨거운 기사 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7302
9


자바 튜토리얼
1622
14


Cakephp 튜토리얼
1342
46


라라벨 튜토리얼
1259
25


PHP 튜토리얼
1207
29



Java에서 return 및 finally 문의 실행 순서는 무엇입니까?

Python에서 'enumerate()' 함수의 목적은 무엇입니까?
