0

我试图阅读一小段网站代码,http://www.site.com/category

我要定位的代码如下所示:

<div class="Brands">
    <h2>Search design</h2>
    <div class="columns">
        <div class="column first">
            <div>
                <a href="/category?Brand=flash">flash</a>
                <span>(9)</span>
            </div>
            <div>
                <a href="/category?Brand=bolt">bolt</a>
                <span>(4)</span> And so on...

我想要做的是阅读a href 地址,然后将名称放在带有2列的表中。
Ex
flash wwwsitecom/category?brand=flash
bolt wwwsitecom//category?brand=bolt

我尝试了几种不同的方法,但不能完全解决它。

<?php
$search = 'columns';
$lines = file('http://www.site.com/category');

// Store true text found
$found = false;
foreach ($lines as $line) {
    if (strpos($line, $search) !== false) {
        $found = true;
        echo $line;
    }
}

// text not found
if (!$found) {
    echo 'No match found';
}
?>

这给了我一个品牌列表,但在每个品牌之后,我希望页面直接链接显示。

有什么想法可以添加该功能吗?

4

1 回答 1

0

我按照你开始逐行解析文件的方式,但你必须确保格式不会改变。这应该为您提供一个关联数组,例如 (BRAND => LINK)。

我使用了explode(),因为您提供的HTML 模式并不难,但如果并非所有链接都遵循此模式,则可能需要进行一些调整(例如,/category?Brand=flash&key=value 会起作用)。

如果它变得更复杂,请看一下如何使用正则表达式。

foreach($lines as $line)
{
  if(strpos($line, $search) !== false)
  {
    $found = true;
    $tmp = explode ('<div>', $line); // -> <a href="/category?Brand=flash">flash</a><span>(9)</span></div>
    $count = count ($tmp);
    for ($i = 1; $count - 1; ++$i) {
      $tmp_href = explode ("\"", $tmp[$i]); // -> $tmp_href[1] = wanted href
      $tmp_brand = explode ('=', $tmp_href); // -> $tmp_brand[1] = wanted brand
      $brand_array[$tmp_brand[1]] = 'http://www.site.com' . $tmp_href[1];
    }
  }
}

如果您想要更可靠的方式,或者如果您要解析大量 HTML 文件以获取链接、品牌等……您应该尝试找到一个好的库来解析 HTML 文件。有很多图书馆在做这件事。

于 2013-08-21T08:58:47.607 回答