一小段PHP代码问题?

来源:百度知道 编辑:UC知道 时间:2024/05/17 04:44:09
<?
$URL = "http://www.microsoft.com/";

//start HTML page
print("<HTML>\n");
print("<HEAD>\n");
print("<TITLE>取得页面的链接</TITLE>\n");
print("</HEAD>\n");

print("<BODY>\n");

$page = fopen($URL, "r");//打开URL

print("链接 $URL<BR>\n");
print("<UL>\n");

while(!feof($page))//在页面中循环
{
$line = fgets($page, 255);
while(ereg("HREF=\"[^\"]*\"", $line, $match))
{
//打印出URL链接
print("<Li>");
print($match[0]);
print("<BR>\n");
}
}

print("</UL>\n");

fclose($page);

//关闭页面<

把ereg改成eregi,ereg是分大小写的,web标准里所有的html标签都该小写,你自然捕捉不到ms的web页里的大写连接了
而且你这一段..是个死循环..posix正则抓取好象最多取十个,这种还是用preg_match_all吧,具体参照下这个吧
<?php
$url = "http://www.microsoft.com/";

//start html page
print("<html>\n");
print("<head>\n");
print("<title>取得页面的链接</title>\n");
print("</head>\n");

print("<body>\n");

$page = file_get_contents($url);//打开url

print("链接 $url<br>\n");
print("<ul>\n");
if(preg_match_all('/href=[\'|"](.+?)[\'|"]/is',$page,$match)){
//打印出url链接
foreach ($match[1] as $v){
print("<li>");
print(substr($v,0,7)!=='http://'?$url.$v:$v);
print("</li>\n