如何在数组中获取元素

时间:2014-04-05 15:10:21

标签: php simple-html-dom

我正在使用我的PHP来搜索元素。有一个名为<p id='links'>的元素,我使用simple_html_dom方法从我的脚本解析名为get-listing.php的包含。

以下是get-listing.php的示例输出:

<p id='channels'>101 ABC FAMILY</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=ABC FAMILY&id=101'>http://www.mysite.com/get-listing.php?channels=ABC FAMILY&id=101</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>102 CBS</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=CBS&id=102'>http://www.mysite.com/get-listing.php?channels=CBS&id=102</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>103 CNN USA</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=CNN USA&id=103'>http://www.mysite.com/get-listing.php?channels=CNN USA&id=103</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>105 ESPN USA</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=ESPN USA&id=105'>http://www.mysite.com/get-listing.php?channels=ESPN USA&id=105</a>
</p>
<a id="aTest" href="rtmp://$OPT:rtmp-raw=rtmp://ny.iguide.to/edge playpath=49f5xnbs2wra0ut swfUrl=http://player.ilive.to/player_ilive_2.swf pageUrl=http://www.ilive.to token=UYDk93k#09sdafjJDHJKAD873">Stream 1</a>
<p id='channels'>106 FOX News</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=FOX News&id=106'>http://www.mysite.com/get-listing.php?channels=FOX News&id=106</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>107 Animal Planet</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=Animal Planet&id=107'>http://www.mysite.com/get-listing.php?channels=Animal Planet&id=107</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>108 USA Network</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=USA Network&id=108'>http://www.mysite.com/get-listing.php?channels=USA Network&id=108</a>
</p>
<a id="aTest" href="">Stream 1</a>

这是我的PHP脚本:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
$link;
include ('simple_html_dom.php');

$base1 = "http://www.mysite.com/get-listing.php";
$html = file_get_html($base1);   

$countp = $html->find('p');     
header("Content-type: text/xml");
$xml .= "<?xml version='1.0' encoding='UTF-8' ?>";
//echo $xml;
$xml .= '<tv generator-info-name="www.testbox.elementfx.com/xmltv">';
?>

我想创建循环以从get-listing.php获取每个数组中的url,其中一个元素为id = links。

你能告诉我怎么做吗?

1 个答案:

答案 0 :(得分:0)

假设simple_html_dom.php按照http://simplehtmldom.sourceforge.net/所述获取您的数据,那么您应该可以使用

foreach to go through the results

$links = $html->find('p[id=links] a');
foreach ($links as $link) {
     //Get raw URL's here
     $urls[] = $link->href;
}

修改

如果你想对href进行排序,你可以在这里进行一些简单的测试

foreach ($links as $link) {
  //Get raw URL's here
  if (strstr($link->href,'get_listing')) {
     $listings[] = $link->href;
  } else {
     $general[] = $link->href;
  }
}