我在將圖像獲取為數組時遇到以下問題。在這段代碼中,我試圖檢查是否Test 1存在要搜索的圖像-如果是,則顯示,如果不顯示,然后嘗試,僅Test 2此而已。當前的代碼可以做到,但是速度非常慢。這if (sizeof($matches[1]) > 3) {是因為3有時它包含爬網網站上的廣告,所以這是我安全的跳過方法。我的問題是我如何才能加快下面的代碼的if (sizeof($matches[1]) > 3) {速度呢?我相信這會使代碼非常慢,因為此數組可能包含多達1000個圖像$get_search = 'Test 1';$html = file_get_contents('https://www.everypixel.com/search?q='.$get_search.'&is_id=1&st=free');preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $html, $matches);if (sizeof($matches[1]) > 3) { $ch_foreach = 1;}if ($ch_foreach == 0) { $get_search = 'Test 2'; $html = file_get_contents('https://www.everypixel.com/search?q='.$get_search.'&is_id=1&st=free'); preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $html, $matches); if (sizeof($matches[1]) > 3) { $ch_foreach = 1; }}foreach ($matches[1] as $match) if ($tmp++ < 20) { if (@getimagesize($match)) { // display image echo $match; }}
- 1 回答
- 0 關注
- 263 瀏覽
添加回答
舉報
0/150
提交
取消