Home > Backend Development > PHP Tutorial > PHP remotely obtains web content

PHP remotely obtains web content

WBOY
Release: 2016-07-29 09:11:16
Original
1049 people have browsed it

1. Intercept the php class file, which mainly includes obtaining the html content of the url and then matching

	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 ;
			}
		}
		
	}
Copy after login
with regular expressions from the html content. The above introduces PHP to remotely obtain web page content, including regular expressions. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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