dtcaw02086 2015-01-28 08:51
浏览 52

PHP - 从HTML数组子项中提取多个信息

I have got this array called $topPaid:

Array
(
    [0] => <li>
        <a class="livelink" href="#%21/content/5664">
            <span title="Relief Terrain Pack v3.2" class="title">Relief Terrain Pack v3.2</span>
            <br><small>
                    Editor Extensions/Terrain
            </small>
            <br></a>
    </li>
    [1] => <li>
        <a class="livelink" href="#%21/content/368">
            <span title="Playmaker" class="title">Playmaker</span>
            <br><small>
                    Editor Extensions/Visual Scripting
            </small>
            <br></a>
    </li>
    [2] => <li>
        <a class="livelink" href="#%21/content/4243">
            <span title="Amplify Motion" class="title">Amplify Motion</span>
            <br><small>
                    Scripting/Effects
            </small>
            <br></a>
    </li>
    [3] => <li>
        <a class="livelink" href="#%21/content/16899">
            <span title="Skele: Character Animation Tools" class="title">Skele: Character Animation Tools</span>
            <br><small>
                    Editor Extensions/Modeling
            </small>
            <br></a>
    </li>
    [4] => <li>
        <a class="livelink" href="#%21/content/19245">
            <span title="SnazzyGrid" class="title">SnazzyGrid</span>
            <br><small>
                    Editor Extensions/Utilities
            </small>
            <br></a>
    </li>
    [5] => <li>
        <a class="livelink" href="#%21/content/19352">
            <span title="Zones, Fields, and Shields" class="title">Zones, Fields, and Shields</span>
            <br><small>
                    Shaders
            </small>
            <br></a>
    </li>
    [6] => <li>
        <a class="livelink" href="#%21/content/18920">
            <span title="PlayerPrefs Elite" class="title">PlayerPrefs Elite</span>
            <br><small>
                    Scripting/Integration
            </small>
            <br></a>
    </li>
    [7] => <li>
        <a class="livelink" href="#%21/content/18358">
            <span title="Bolt" class="title">Bolt</span>
            <br><small>
                    Scripting/Network
            </small>
            <br></a>
    </li>
    [8] => <li>
        <a class="livelink" href="#%21/content/13198">
            <span title="BIG Environment Pack Vol.3" class="title">BIG Environment Pack Vol.3</span>
            <br><small>
                    3D Models/Environments
            </small>
            <br></a>
    </li>
    [9] => <li>
        <a class="livelink" href="#%21/content/23930">
            <span title="VertExmotion" class="title">VertExmotion</span>
            <br><small>
                    Editor Extensions/Animation
            </small>
            <br></a>
    </li>
)

Now I try to fetch out the "href" link, the "title" and the text in "small" to show them in a table with this code:

foreach($topPaid as $key => $value)
{
    $xml = simplexml_load_string($key);
    $list = $xml->xpath("//@href");
    $preparedUrls = array();
    foreach($list as $item) {
        $item = parse_url($item);
        $preparedUrls[] = $item['scheme'] . '://' .  $item['host'] . '/';
    }
    print_r($preparedUrls);
}

But I always get the error that I try to access a non-member object. Should I parse each array element by line and parse it's line content or what is a better method to reach the information?

Greetz,

  • 写回答

2条回答 默认 最新

  • douyangquan2474 2015-01-28 08:57
    关注

    I did not try your code but it seems to me that error is in line:

    $xml = simplexml_load_string($key);
    

    should be:

    $xml = simplexml_load_string($value);
    

    and text you are passing to the function is not valid XML, <br> should be <br />.

    评论

报告相同问题?

悬赏问题

  • ¥15 icb slave转ahb master总线设计 能够接入arm busmatrix
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了