小偷程序:把远程网站上的数据(图片,网页及其他文件)抓取到本地,处理后再显示
正则表达式: 用于字符串的模式分割 、匹配、查找及替换操作。
正则表达式: 用于字符串的模式分割 、匹配、查找及替换操作。
相关函数:
int ereg ( string
$pattern
, string $string
[, array &$regs
] )
若省略参数返回的数组,找到则返回值为 True 否则 返回 False
与之对应
eregi() 不区分大小写。
string
file_get_contents
(
string
$filename
[,
bool $use_include_path
= false
[,
resource $context
[,
int $offset
= 0
[,
int $maxlen
]]]] )
读取整个文件,比如:
用此函数可以获取网页信息
他就是小偷程序的基础。
比如:
<?php
$url=file_get_contents("
http://www.ubuntu.org.cn/index_kylin");
echo $url;
?>
但是对于另一个网站:
<?php
$url=file_get_contents("
http://www.alangzhong.com/index.html");
echo $url;
?>
发现很多的背景图片是看不见的。
查看网页源代码我们发现,这是
<img width="116" height="98"
src="/upload/201503/b123ec26-bb8f-43be-b5ad-cdf45153d053.png"/>
图片的地址使用了相对路径,而我们本地没有这样的文件,当然显示不出来。
用正则表达式选定图片,然后远程地址替换相对路径:
下面代码的超时问题没有解决。
<?php
//ini_set('max_execution_time', '0'); //三者都没用啊,一直超时
//@ini_set('default_socket_timeout', 20000);
//set_time_limit(2);
$url=file_get_contents("http://www.alangzhong.com/index.html");
//echo $url;
$fp = @fopen($url, "r") or die("超时"); //为什么不断超时
$contents = file_get_contents($url);
eregi("<img width=\"116\" height=\"98\" src=\"/upload/201503/b123ec26-bb8f-43be-b5ad-cdf45153d053.png\"/>",$contents,$rg);
// 远程地址替换相对路径
$rg[1]=str_replace("src=\"../upload/","src=\"http://www.alangzhong.com/index.html/upload/",$rg[1]);
echo $rg[1];
?>