dongzhou4727 2014-11-23 08:09
浏览 38
已采纳

PHP - 无限循环的问题

I have no clue what I am doing wrong. EVERY time it goes through it just keeps looping and pulling all of the cities listed in the same row and placing them with the state, then when it goes to the next state it starts at the correct place, but still keeps going. I've tried for 4 hours I just can't figure it out.

$url = 'http://www.craigslist.org/about/sites';
$output = file_get_contents($url); 
$doc = new DOMDocument();

libxml_use_internal_errors(true); //Supress Warnings for HTML5 conversion issue
$doc->loadHTML($output);
libxml_use_internal_errors(false); //Start Showing Errors

$xpath = new DOMXpath($doc);    

foreach ($xpath->query('//h1') as $e) {
    $country = $e->nodeValue;
    $list = array();
    foreach ($xpath->query('./following-sibling::div[@class="colmask"]', $e) as $li) {

        foreach ($xpath->query('//div/h4', $e) as $div) {
            $state = $div->nodeValue;


            foreach ($xpath->query('./following-sibling::ul/li', $div) as $div2) {                      

                $href =  $div2->getAttribute("href");
                $text = trim(preg_replace("/[
]+/", " ", $div2->nodeValue));
                echo 'Country: ' . $country . ' State: ' . $state . ' CITY: text['. $text . '] href[' . $href . '] <br/><br/><br/>';


            }


        }

    }
}  
  • 写回答

2条回答 默认 最新

  • duanhuan6336 2014-11-24 00:50
    关注

    Had someone named DuffyDake answer my question. Here is the answer..

    foreach ($xpath->query('./following-sibling::ul[1]/li', $div) as $div2) {                      
    
                    $href =  $div2->getAttribute("href");
                    $text = trim(preg_replace("/[
    ]+/", " ", $div2->nodeValue));
                    echo 'Country: ' . $country . ' State: ' . $state . ' CITY: text['. $text . '] href[' . $href . '] <br/><br/><br/>';
    
    
                }
    

    The missing piece is the [1] to reference to the first UL found and not anything past that

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示