求大神。。。

[复制链接]
查看: 519   回复: 5
发表于 2014-10-21 21:56:41 | 显示全部楼层 |阅读模式
20T币
本帖最后由 xx19941215 于 2014-10-21 22:02 编辑

代码[mw_shl_code=php,true]<?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("'([rn])+'","",$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[0][0]);//执行函数
  print_r($arr);
?>
[/mw_shl_code] 函数是将表格转换为数组的函数,网上找的。。问题是:


附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册

最佳答案

查看完整内容

楼主结账! 人呢

点评

不好意思,这里大神多,就来这里问了,小弟php,正则,基本啥都不会,着这些代码还费了2个小时  发表于 2014-10-21 22:03
回复

使用道具 举报

发表于 2014-10-21 21:56:42 | 显示全部楼层
楼主结账!                      人呢
回复

使用道具 举报

 楼主| 发表于 2014-10-21 22:14:25 | 显示全部楼层
[mw_shl_code=php,true]<?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("'([rn])+'","",$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[0][0]);//执行函数
  print_r($arr);
?>
[/mw_shl_code]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

 楼主| 发表于 2014-10-21 22:15:00 | 显示全部楼层
一样的代码,上面的这个就输出了完全的数组求大神。。。求大神。。。求大神。。。
回复

使用道具 举报

发表于 2014-10-22 13:52:01 | 显示全部楼层
[mw_shl_code=php,true]<!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("'([rn])+'","",$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[0][0]);//执行函数
print_r($arr);
?>
</body>
</html>
[/mw_shl_code]

已调整完全正确输出,给楼主你注释了哪句代码的问题

评分

参与人数 1T币 +13 收起 理由
月光飞燕 + 13 認眞參與

查看全部评分

回复

使用道具 举报

发表于 2014-10-22 13:53:15 | 显示全部楼层
求大神。。。记得给悬赏!  20分啊,口水流出来了

点评

好的,终于来人了啊,我测试看看  发表于 2014-10-22 14:00
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则