0

このパーサーをより効率的にするにはどうすればよいですか? 私はこれらのように感じます if ステートメントはクレイジーです!私は、コールバック関数が仕事を成し遂げることができると考えています。ただし、ほとんどの識別子は大きく異なり、さまざまなキーを使用する必要があります。タグの配列と DOM 要素の配列を作成し、それぞれに対して null 値を削除するコールバック関数を作成する必要がありますか? 初めてスクレーパーを組み立てようとしていますが、ここのロジックに本当に困惑しています。

どんな助けでも大歓迎です!

foreach($html->find('.b-card') as $article) {
    $data[$y]['business']     = $article->find('h1', 0)->plaintext;
    $data[$y]['address']      = $article->find('.address', 0)->plaintext;

    if($article->find('.phone-num', 0)) {
      $data[$y]['phone']      = $article->find('.phone-num', 0)->plaintext;
    } else {
       $data[$y]['phone']     = 'Number not listed.';
    }

    if($article->find('.link', 0)) {
      $data[$y]['website']    = $article->find('.link', 0)->plaintext;
    } else {
       $data[$y]['website']   = 'Website not listed.';
    }
    if($article->find('.established', 0)) {
      $data[$y]['years']    = str_replace("\r\n","",$article->find('.established', 0)->plaintext);
    } else {
       $data[$y]['years']   = 'Years established not listed.';
    }
    if($article->find('.open-hours', 0)) {
      $data[$y]['hours']    = $article->find('.open-hours', 0)->plaintext;
    } else {
       $data[$y]['hours']   = 'Hours not listed.';
    }
    if($article->find('.categories a', 0)) {
      $data[$y]['category']    = $article->find('.categories a', 0)->plaintext;
    } else {
       $data[$y]['category']   = 'Category not listed.';
    }

    $articles[] = $data[$y];
}

}

こんなこともできそうな気がする

function my_callback($element) {
        // remove all null tags 
        if ($element->tag)
                $article->find....;
} 
4

1 に答える 1