簡體   English   中英

我如何在不重復自己的情況下更好地編寫此語句,它使用簡單的 html dom 調用值

[英]How can i write this statement better without repeating myself, it calls for values using simple html dom

我有這個塊代碼,它工作正常,但我覺得我重復了很多,當他們回來時它會減慢結果。 我正在使用簡單的 html dom 來獲取結果。 我怎樣才能盡可能有效地做到這一點?

$html = file_get_html( $urlfix );

// now lets go through all results to see what we get

$links = array();
foreach ( $html->find( 'div.mozaique div.thumb-block ' ) as $a ) {
    $links[] = $a->id;
}

// this will return  images

$datasrc = 'data-src';
$links2 = array();
foreach ( $html->find( 'div.mozaique div.thumb-block img ' ) as $a ) {
    $links2[] = $a->$datasrc;
}

// this will return the links


$links3 = array();
foreach ( $html->find( 'div.mozaique div.thumb-block div.thumb a ' ) as $a ) {
    $links3[] = $a->href;
}

// print_r($links3);

$links4 = array();
foreach ( $html->find( 'div.mozaique div.thumb-under p.title a ' ) as $a ) {
    $links4[] = $a->title;
}

// this will return another set of links

$vidoid = 'data-id';

$links5 = array();
foreach ( $html->find( 'div.mozaique div.thumb-block  ' ) as $a ) {
    $links5[] = $a->$vidoid;
}

使它成為一個函數並多次調用它。

例如:

function get_list($html, $find, $element) {
    $list = array();
    foreach ( $html->find($find) as $a ) {
        $list[] = $a->$element;
    }
    return $list;
}

$links = get_list($html, 'div.mozaique div.thumb-block ', 'id');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM