Home > Backend Development > PHP Tutorial > 关于PHP正则匹配取出数据的有关问题

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

WBOY
Release: 2016-06-13 12:07:51
Original
997 people have browsed it

关于PHP正则匹配取出数据的问题
下面是经过一部分处理的源代码

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

我想要的是取出下面这一段数据放到数组里,不知道该怎么写了,第一次接触正则感觉看不懂,谢谢各位大神们!
[19] => <br />编号{td}<br />课程{td}<br />日期{td}<br />时间{td}<br />班级{td}<br />考场{td}<br />任课教师{td}<br /><br />[20] => <br />1{td}<br />流体力学{td}<br />2014-11-0600:00:00.0<br />{td}<br />14:00-16:00{td}<br />土木122{td}<br />Ⅲ-209{td}<br />巩妮娜{td}
Copy after login

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

还是这样的
print_r(array_combine(explode('{td}', $exam[19]), explode('{td}', $exam[20])));
Copy after login

Array<br>(<br>    [编号] => 1<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template