求大神。。。
本帖最后由 xx19941215 于 2014-10-21 22:02 编辑代码<?php
function get_td_array($table) {
$table = preg_replace("'<table[^>]*?>'si","",$table);
$table = preg_replace("'<tr[^>]*?>'si","",$table);
$table = preg_replace("'<td[^>]*?>'si","",$table);
$table = str_replace("</tr>","{tr}",$table);
$table = str_replace("</td>","{td}",$table);
//去掉 HTML 标记
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
//去掉空白字符
$table = preg_replace("'()+'","",$table);
$table = preg_replace('/ /',"",$table);
$table = str_replace(" ","",$table);
$table = str_replace(" ","",$table);
$table = explode('{tr}', $table);
array_pop($table);
foreach ($table as $key=>$tr) {
$td = explode('{td}', $tr);
array_pop($td);
$td_array[] = $td;
}
return $td_array;
}
//下载HTML网页
$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
$s=mb_convert_encoding("$s", "UTF-8", "GBK");//编码转换
//获取最后一个table内容
$s=substr($s,strrpos($s,'<table'));
$s=substr($s,0,strpos($s,'</table>')+8);
//删除超链接
$s=preg_replace('|<a href.*?</a>|','',$s);
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//删除多余空格
echo $s;
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正则表达式将课表的表格取出
$arr = get_td_array($table);//执行函数
print_r($arr);
?>
函数是将表格转换为数组的函数,网上找的。。问题是:
楼主结账! 人呢
<?php
header('Content-Type:text/html;charset:Utf-8');
function get_td_array($table) {
$table = preg_replace("'<table[^>]*?>'si","",$table);
$table = preg_replace("'<tr[^>]*?>'si","",$table);
$table = preg_replace("'<td[^>]*?>'si","",$table);
$table = str_replace("</tr>","{tr}",$table);
$table = str_replace("</td>","{td}",$table);
//去掉 HTML 标记
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
//去掉空白字符
$table = preg_replace("'()+'","",$table);
$table = preg_replace('/ /',"",$table);
$table = str_replace(" ","",$table);
$table = str_replace(" ","",$table);
$table = explode('{tr}', $table);
array_pop($table);
foreach ($table as $key=>$tr) {
$td = explode('{td}', $tr);
array_pop($td);
$td_array[] = $td;
}
return $td_array;
}
//下载HTML网页
$s=file_get_contents('http://222.88.107.92/exam/query/query_detail.jsp?examid=2014102014&paperid=100094');
$s=mb_convert_encoding("$s", "UTF-8", "GBK");//编码转换
//获取最后一个table内容
$s=substr($s,strrpos($s,'<table'));
$s=substr($s,0,strpos($s,'</table>')+8);
//删除超链接
$s=preg_replace('|<a href.*?</a>|','',$s);
$s=preg_replace('|<td align="center" width="120">图片</td>|','',$s);
$s=preg_replace('|<td align=\'center\'></td>|','',$s);
$s=preg_replace('|<td align="center" width="120">评阅老师</td>|','',$s);
$s=preg_replace('|<td align=\'center\'>110(.*?)<br></td>|','',$s);
echo $s;
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正则表达式将课表的表格取出
$arr = get_td_array($table);//执行函数
print_r($arr);
?>
一样的代码,上面的这个就输出了完全的数组:(:(:(
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试</title>
</head>
<body>
<?php
function get_td_array($table) {
//去掉table标签上的属性
$table = preg_replace("'<table[^>]*?>'si","",$table);
//去掉TR上的属性
$table = preg_replace("'<tr[^>]*?>'si","",$table);
//去掉td上的属性
$table = preg_replace("'<td[^>]*?>'si","",$table);
//标签替换
$table = str_replace("</tr>","{tr}",$table);
$table = str_replace("</td>","{td}",$table);
//去掉 HTML 标记
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
//去掉空白字符
$table = preg_replace("'()+'","",$table);
$table = preg_replace('/ /',"",$table);
$table = str_replace(" ","",$table);
$table = str_replace(" ","",$table);
$table = explode('{tr}', $table);
//这里打印出来是长度为2的数组。
print_r($table);
//这句代码有问题,这个会把数组的第二个元素删掉,所以得不到想要的结果
//array_pop($table);
$td_array = array();
foreach ($table as $key=>$tr) {
$td = explode('{td}', $tr);
array_pop($td);
$td_array[] = $td;
}
return $td_array;
}
//下载HTML网页
$s=file_get_contents('http://chengji.lideping.com:7000/exam/query/query.jsp?logname=20133011235');
$s=mb_convert_encoding("$s", "UTF-8", "GBK");//编码转换
//获取最后一个table内容
$s=substr($s,strrpos($s,'<table'));
$s=substr($s,0,strpos($s,'</table>')+8);
//删除超链接
$s=preg_replace('|<a href.*?</a>|','',$s);
$s=preg_replace('|<td align=\'center\'></td>|','',$s);//删除多余空格
echo $s;
preg_match_all('/<table [^>]*>([\s\S]*?)<\/table>/',$s,$table);//用正则表达式将课表的表格取出
$arr = get_td_array($table);//执行函数
print_r($arr);
?>
</body>
</html>
已调整完全正确输出,给楼主你注释了哪句代码的问题
;P记得给悬赏!20分啊,口水流出来了
页:
[1]