• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于PHP正则匹配取出数据的问题

php 搞代码 3年前 (2022-01-23) 13次浏览 已收录 0个评论
文章目录[隐藏]

下面是经过一部分处理的源代码

<?php$exam = curl_init("http://exam.hhit.edu.cn/fgquery.do?status=lowquery&tsid=2012120348");curl_setopt($exam, CURLOPT_RETURNTRANSFER, true); // 获取数据返回curl_setopt($exam, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回$exam=curl_exec($exam);        $exam = preg_replace("'<table>]*?>'si","",$exam);          $exam = preg_replace("'<tr>]*?>'si","",$exam);           $exam = preg_replace("'<td>]*?>'si","",$exam);           $exam = str_replace("</tr>","{tr}",$exam);           $exam = str_replace("</td>","{td}",$exam);           //去掉 HTML 标记            $exam = preg_replace("'<[/!]*?[^]*?>'si","",$exam);          //去掉空白字符             $exam = preg_replace("'([rn])[s]+'","",$exam);        $exam = preg_replace('/ /',"",$exam);           $exam = str_replace(" ","",$exam);           $exam = str_replace(" ","",$exam);         $exam = explode('{tr}', $exam);        array_pop($exam);               print_r($exam);?>

我想要的是取出下面这一段数据放到数组里,不知道该怎么写了,第一次接触正则感觉看不懂,谢谢各位大神们!

[19] => 编号{td}课程{td}日期{td}时间{td}班级{td}考场{td}任课教师{td}[20] => 1{td}流体力学{td}2014-11-0600:00:00.0{td}14:00-16:00{td}土木122{td}Ⅲ-209{td}巩妮娜{td}

回复讨论(解决方案)

你是要这样的

print_r(array_map(null, explode('{td}', $exam[19]), explode('{td}', $exam[20])));
Array(    [0] => Array        (            [0] => 编号            [1] => 1        )    [1] => Array        (            [0] => 课程            [1] => 流体力学        )    [2] => Array        (            [0] => 日期            [1] => 2014-11-0600:00:00.0        )    [3] => Array        (            [0] => 时间            [1] => 14:00-16:00        )    [4] => Array        (            [0] => 班级            [1] => 土木122        )    [5] => Array        (            [0] => 考场            [1] => Ⅲ-209        )    [6] => Array        (            [0] => 任课教师            [1] => 巩妮娜        )    [7] => Array        (            [0] =>             [1] =>         ))

还是这样的

print_r(array_combine(explode('{td}', $exam[19]), explode('{td}', $exam[20])));
Array(    [编号] => 1    [课程] => 流体力学    [日期] => 2014-11-0600:00:00.0    [时间] => 14:00-16:00    [班级] => 土木122    [考场] => Ⅲ-209    [任课教师] => 巩妮娜    [] => )

我想应该是第二种 作为一个数组变量插入到模板中 谢谢大神

版主,可以问个小问题吗?[日期] => 2014-11-0600:00:00.0这个数据在页面上是没有00:00:00.0这些的,能去掉吗?

$s = '2014-11-0600:00:00.0'<strong>)本文来(源gaodai#ma#com搞@@代~&码*网2</strong><pre>搞代gaodaima码

;echo date(‘Y-m-d’, strtotime($s));echo substr($s, 0, 10);
两种方法都可以

谢谢版主,如此敬业,感激万分,此贴终结!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于PHP正则匹配取出数据的问题
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址