關於PHP正則匹配取出資料的問題

來源:互聯網
上載者:User
下面是經過一部分處理的原始碼
]*?>'si","",$exam);          $exam = preg_replace("']*?>'si","",$exam);           $exam = preg_replace("']*?>'si","",$exam);           $exam = str_replace("","{tr}",$exam);           $exam = str_replace("","{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';echo date('Y-m-d', strtotime($s));echo substr($s, 0, 10);

兩種方法都可以

謝謝版主,如此敬業,感激萬分,此貼終結!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.