ホームページ  >  記事  >  バックエンド開発  >  PHP正規マッチングによるデータ抽出の問題について

PHP正規マッチングによるデータ抽出の問題について

WBOY
WBOYオリジナル
2016-06-23 13:47:42881ブラウズ

以下は部分的に処理されたソースコードです

<?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    [任课教师] => 巩妮娜    [] => )

2 番目に配列変数として挿入する必要があると思います。神様、ありがとうございます

司会者、ちょっと質問してもいいですか? [日付] => 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);

どちらの方法でもOKです

モデレーターの献身的なご対応に感謝し、この投稿は終了です。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。