1

私はこのコードを持っています:

<?php

$pattern = "/<span\b[^>]*>(.*?)</";
$html = file_get_contents("http://www.bicicletapublica.com.ar/mapa.aspx");
$results = array();
preg_match_all($pattern, $html, $results );
$results  = $results[0];
$stations = array();

for($i = 0; $i < count($results)-1;$i++){
    if(!($i & 1)){
        $key = strtolower(str_replace(" ","_",substr($results[$i], 0, -1)));
        $stations["$key"] = str_replace("Cant. Bicicletas disponibles: ","",substr($results[$i+1], 0, -1));

    }
}
print_r($stations);
print_r($stations["retiro"])
?>  

そして、私はこのエラーが発生しています:

Array ( [retiro] => 40 [aduana] => 26 [derecho] => 27 [plaza_roma] => 8 [plaza_italia] => 29 [parque_lezama] => 31 [obelisco] => 28 [congreso] => 7 [parque_las_heras] => 17 [uca_puerto_madero] => 27 [tribunales] => 25 [plaza_vicente_lopez] => 23 [once] => 27 [pacifico] => 19 [virrey_cevallos] => 26 [plaza_houssay] => 2 [plaza_de_mayo] => 6 [plaza_almagro] => 21 [cmd] => 7 [independencia] => 9 [plaza_san_martin] => 21 )

Notice: Undefined index: retiro in /opt/lampp/htdocs/mejorenbici/index.php on line 17

ご覧のとおり、キーretiroは定義されていますが、未定義のインデックス エラーが発生する理由がわかりません。

4

2 に答える 2

3

通知のHTML ソースコードを見てください( not : error )。を使用してコンソールでコードを実行すると、次のようになります。php -a

Array (
  [<span_class="style1">retiro] => <span class="style2">40
  [<span_class="style1">aduana] => <span class="style2">26
  [<span_class="style1">derecho] => <span class="style2">27
  [<span_class="style1">plaza_roma] => <span class="style2">8
  [<span_class="style1">plaza_italia] => <span class="style2">29
  [<span_class="style1">parque_lezama] => <span class="style2">31
  [<span_class="style1">obelisco] => <span class="style2">28
  [<span_class="style1">congreso] => <span class="style2">7
  [<span_class="style1">parque_las_heras] => <span class="style2">17
  [<span_class="style1">uca_puerto_madero] => <span class="style2">27
  [<span_class="style1">tribunales] => <span class="style2">25
  [<span_class="style1">plaza_vicente_lopez] => <span class="style2">23
  [<span_class="style1">once] => <span class="style2">27
  [<span_class="style1">pacifico] => <span class="style2">19
  [<span_class="style1">virrey_cevallos] => <span class="style2">26
  [<span_class="style1">plaza_houssay] => <span class="style2">2
  [<span_class="style1">plaza_de_mayo] => <span class="style2">6
  [<span_class="style1">plaza_almagro] => <span class="style2">21
  [<span_class="style1">cmd] => <span class="style2">7
  [<span_class="style1">independencia] => <span class="style2">9
  [<span_class="style1">plaza_san_martin] => <span class="style2">21
)
PHP Notice: Undefined index: retiro in - on line 19
PHP Stack trace:
PHP   1. {main}() -:0

1 つの正規表現を 1 回だけ適用して HTML を解析しないでください。マークアップ パーサーを使用します (効率を高めるために正規表現を利用できます)。

そのデータを再利用する許可はありますか?

于 2011-11-12T21:59:46.470 に答える
1

配列に空のインデックスを入力していないことを確認するために、ステーション配列をラップする必要があります。あなたのエラーはおそらく前の print_r() ステートメントが原因です:

print_r($ステーション);

新しいロジック ラッピング:

if (!empty($key)) { $stations["$key"] = str_replace("Cant. Bicicletas disponibles: ","",substr($results[$i+1], 0, -1)); }

これにより、配列内の空のインデックスが防止されます。

于 2011-11-12T21:56:23.647 に答える