php parse html data from specific positions - php

I have a lot of these in my html document:
<div class="thumb-under">
<p class="title">
text
</p>
<p class="metadata">
<span class="bg">
<span class="first">3A
</span>
<a href="4A">
<span class="name">5A
</span></a>
<span>
<span class="spring"> -
</span> 6A
<span class="spring">something
</span>
</span>
<span class="spring"> -
</span>
</span>
</p>
</div>
so I need to extract data from positions 1A, 2A, 3A, 4A, 5A, 6A
I tried this but something I am doing wrong:
$matches = array();
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach($dom->getElementsByTagName('p') as $tr) {
if ( ! $tr->hasAttribute('class')) {
continue;
}
$class = explode(' ', $tr->getAttribute('class'));
if (in_array('title', $class)) {
$matches[] = $tr->getElementsByTagName('a');
}
}
print_r($matches);
I am totally lost...

Related

Grabbing specific elements inside DIV from external page

I need to scrap the following elements inside each one of these div's class="product-grid-item" (page contains several of them), but in fact I have no clue how to do it... so, I need help not to pull my hair out.
1 - The link and image inside the div: class="product-element-top2;
<a href="https://...this_link" class="product-image-link"> (just need the link)
<img width="300" height="300" src="https://...this_image_url... (just need this image URL)
2 - The title inside the h3 tag as follows;
<h3 class="wd-entities-title"><a href="https://...linkhere">The title goes here (just the title)
3 - Last but not least, I need to grab tha price inside this;
<span class="price"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">€</span>20,00</bdi></span></span> (just the "€20.00")
Here's the full HTML:
<div class="product-grid-item" data-loop="1">
<div class="product-element-top">
<a href="https://...linkhere" class="product-image-link">
<img width="300" height="300" src="https://image-goes-here.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail"> </a>
<div class="top-information wd-fill">
<h3 class="wd-entities-title">The title goes here</h3>
<span class="price"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">€</span>20,00</bdi></span></span>
<div class="wd-add-btn wd-add-btn-replace woodmart-add-btn">
<span>Options</span></div>
</div>
<div class="wd-buttons wd-pos-r-t color-scheme-light woodmart-buttons">
<div class="wd-compare-btn product-compare-button wd-action-btn wd-style-icon wd-compare-icon">
Buy
</div>
<div class="quick-view wd-action-btn wd-style-icon wd-quick-view-icon wd-quick-view-btn">
quick view
</div>
<div class="wd-wishlist-btn wd-action-btn wd-style-icon wd-wishlist-icon woodmart-wishlist-btn">
<a class="" href="https://linkhere/wishlist/" data-key="dcf36756534755" data-product-id="387654" data-added-text="See Wishlist">Wishlist</a>
</div>
</div>
<div class="quick-shop-wrapper wd-fill wd-scroll">
<div class="quick-shop-close wd-action-btn wd-style-text wd-cross-icon">Close</div>
<div class="quick-shop-form wd-scroll-content">
</div>
</div>
</div>
</div>
One of my clumsy attempts:
$html = file_get_contents("https://url-here.goetohere");
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'product-grid-item';
$classname = 'product-element-top2';
$classname = 'product-element-top2';
$classname = 'wd-entities-title';
$classname = 'price';
$nodes = $finder->query("//*[contains(#class, '$classname')]");
foreach ($nodes as $node) {
echo 'here »» ' . htmlentities($node->nodeValue) . '<br>';
}
Assuming that the HTML is being fetched correctly prior to attempting any DOM processing then it is fairly straightforward to construct some basic XPath expressions to find the indicated content.
As per the comment page contains several of them there are 2 product-grid-item divs as you'll note in the output.
$html='
<div class="product-grid-item" data-loop="1">
<div class="product-element-top">
<a href="https://...linkhere" class="product-image-link">
<img width="300" height="300" src="https://image-goes-here.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail">
</a>
<div class="top-information wd-fill">
<h3 class="wd-entities-title">
The title goes here
</h3>
<span class="price">
<span class="woocommerce-Price-amount amount">
<bdi>
<span class="woocommerce-Price-currencySymbol">€</span>20,00
</bdi>
</span>
</span>
<div class="wd-add-btn wd-add-btn-replace woodmart-add-btn">
<a href="https://...linkhere" data-quantity="1" class="button product_type_variable add_to_cart_button add-to-cart-loop">
<span>Options</span>
</a>
</div>
</div>
<div class="wd-buttons wd-pos-r-t color-scheme-light woodmart-buttons">
<div class="wd-compare-btn product-compare-button wd-action-btn wd-style-icon wd-compare-icon">
Buy
</div>
<div class="quick-view wd-action-btn wd-style-icon wd-quick-view-icon wd-quick-view-btn">
quick view
</div>
<div class="wd-wishlist-btn wd-action-btn wd-style-icon wd-wishlist-icon woodmart-wishlist-btn">
<a class="" href="https://linkhere/wishlist/" data-key="dcf36756534755" data-product-id="387654" data-added-text="See Wishlist">Wishlist</a>
</div>
</div>
<div class="quick-shop-wrapper wd-fill wd-scroll">
<div class="quick-shop-close wd-action-btn wd-style-text wd-cross-icon">
Close
</div>
<div class="quick-shop-form wd-scroll-content"></div>
</div>
</div>
</div>
<div class="product-grid-item" data-loop="1">
<div class="product-element-top">
<a href="https://www.example.com/banana" class="product-image-link">
<img width="300" height="300" src="https://www.example.com/kittykat.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail">
</a>
<div class="top-information wd-fill">
<h3 class="wd-entities-title">
Oh look, another title!
</h3>
<span class="price">
<span class="woocommerce-Price-amount amount">
<bdi>
<span class="woocommerce-Price-currencySymbol">€</span>540,00
</bdi>
</span>
</span>
<div class="wd-add-btn wd-add-btn-replace woodmart-add-btn">
<a href="https://www.example.com/gorilla" data-quantity="1" class="button product_type_variable add_to_cart_button add-to-cart-loop">
<span>Options</span>
</a>
</div>
</div>
<div class="wd-buttons wd-pos-r-t color-scheme-light woodmart-buttons">
<div class="wd-compare-btn product-compare-button wd-action-btn wd-style-icon wd-compare-icon">
Buy
</div>
<div class="quick-view wd-action-btn wd-style-icon wd-quick-view-icon wd-quick-view-btn">
quick view
</div>
<div class="wd-wishlist-btn wd-action-btn wd-style-icon wd-wishlist-icon woodmart-wishlist-btn">
<a class="" href="https://www.example.com/wishlist/" data-key="dcf36756534755" data-product-id="387654" data-added-text="See Wishlist">Wishlist</a>
</div>
</div>
<div class="quick-shop-wrapper wd-fill wd-scroll">
<div class="quick-shop-close wd-action-btn wd-style-text wd-cross-icon">
Close
</div>
<div class="quick-shop-form wd-scroll-content"></div>
</div>
</div>
</div>';
To process the downloaded HTML
# set the libxml parameters and create new DOMDocument/XPath objects.
libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->strictErrorChecking=false;
$dom->recover=true;
$dom->loadHTML( $html );
libxml_clear_errors();
$xp=new DOMXPath( $dom );
# some basic XPath expressions
$exprs=(object)array(
'product-link' => '//a[#class="product-image-link"]',
'product-img-src' => '//a[#class="product-image-link"]/img',
'h3-title-text' => '//h3[#class="wd-entities-title"]',
'price' => '//span[#class="price"]/span/bdi'
);
# find the keys (for convenience) to be used below
$keys=array_keys( get_object_vars( $exprs ) );
# store results here
$res=array();
# loop through all patterns and issue XPath query.
foreach( $exprs as $key => $expr ){
# add key to output and set as an array.
$res[ $key ]=[];
$col=$xp->query( $expr );
# find the data if the query succeeds
if( $col && $col->length > 0 ){
foreach( $col as $node ){
switch( $key ){
case $keys[0]:$res[$key][]=$node->getAttribute('href');break;
case $keys[1]:$res[$key][]=$node->getAttribute('src');break;
case $keys[2]:$res[$key][]=trim($node->textContent);break;
case $keys[3]:$res[$key][]=trim($node->textContent);break;
}
}
}
}
# show the result or do really interesting things with the data
printf('<pre>%s</pre>',print_r($res,true));
Which yields:
Array
(
[product-link] => Array
(
[0] => https://...linkhere
[1] => https://www.example.com/banana
)
[product-img-src] => Array
(
[0] => https://image-goes-here.jpg
[1] => https://www.example.com/kittykat.jpg
)
[h3-title-text] => Array
(
[0] => The title goes here
[1] => Oh look, another title!
)
[price] => Array
(
[0] => â¬20,00
[1] => â¬540,00
)
)

DOMXPATH Evaluate string with variable nested position

I've been using DOMXPATH and I love it, but I need it to be a little more intuitive.
Some clients add some extra HTML in their code, which screws up our project.
Example 1:
<div id="Fooen">
<span class="FooTitle">Overdracht</span>
<span class="Foo koopprijs">
<span class="FooName">Vraagprijs</span>
<span class="FooValue">€ 299.000,-</span>
</span>
<span class="Foo aanvaarding">
<span class="FooName">Aanvaarding</span>
<span class="FooValue">In overleg</span>
</span>
</div>
We can get the SPAN name and values fine with this:
$filtered = $domxpath->query("//div[#class='Fooen']/span");
foreach ($filtered as $myItem) {
$temp_name = $domxpath->evaluate("string(descendant::span[#class='FooName'])", $myItem);
$name = strtolower(preg_replace('/\s*/', '', $temp_name));
$value = $domxpath->evaluate("string(descendant::span[#class='FooValue'])", $myItem);
}
But, sometimes the client added code, so the nodes are now deeper. I cannot seem to find an answer to this without mapping it all the way down.
Example 2:
<div id="Fooen">
<div>
<div class="blok-sizer"></div>
<div id="" class="block">
<div class="top">
<div class="center column"></div>
</div>
<div class="middle">
<div class="center column">
<span class="FooTitle">Overdracht</span>
<span class="Foo first transactiestatus">
<span class="FooName">Status</span>
<span class="FooValue">Beschikbaar</span>
</span>
<span class="Foo koopprijs">
<span class="FooName">Vraagprijs</span>
<span class="FooValue">€ 975.000,-</span>
</span>
</div>
</div>
</div>
</div>
</div>
But now, this won't work:
$filtered = $domxpath->query("//div[#class='Fooen']/span");
foreach ($filtered as $myItem) {
$temp_name = $domxpath->evaluate("string(descendant::span[#class='FooName'])", $myItem);
$name = strtolower(preg_replace('/\s*/', '', $temp_name));
$value = $domxpath->evaluate("string(descendant::span[#class='FooValue'])", $myItem);
}
I have tried variations like these:
$domxpath->evaluate("string(descendant::*[#class='FooName'])", $myItem);
$domxpath->evaluate("string(//*[#class='FooName'])", $myItem);
$domxpath->evaluate("string(*[#class='FooName'])", $myItem);
$domxpath->evaluate("string(.//span[#class='FooName'])", $myItem);
Is there a way to get the outcome of a string, even if it is not at the same place each time, thus more flexible?
Edit, here is a ready to copy/paste sample I am currently working with. First is the working one, second is the one I'd like to get working from root to end and not fixed but flexible. If I knew how to fiddle, I would, sorry.
<?php
function getDom($url = "")
{
$str = $url;
$internalErrors = libxml_use_internal_errors(true);
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($str);
libxml_use_internal_errors($internalErrors);
return $dom;
}
$domcode = '<div class="Fooen">
<span class="FooTitle">Overdracht</span>
<span class="Foo koopprijs">
<span class="FooName">Vraagprijs</span>
<span class="FooValue">€ 299.000,-</span>
</span>
<span class="Foo aanvaarding">
<span class="FooName">Aanvaarding</span>
<span class="FooValue">In overleg</span>
</span>
</div>';
$dom = getDom($domcode);
$html = '';
$domxpath = new \DOMXPath($dom);
$newDom = new \DOMDocument;
$newDom->formatOutput = true;
$filtered = $domxpath->query("//div[#class='Fooen']/span");
foreach ($filtered as $myItem) {
$temp_name = $domxpath->evaluate("string(descendant::span[#class='FooName'])", $myItem);
echo strtolower(preg_replace('/\s*/', '', $temp_name));
echo " = ";
echo $domxpath->evaluate("string(descendant::span[#class='FooValue'])", $myItem);
echo "<br>";
}
echo "<br>";
$domcode = '
<div class="Fooen">
<div>
<div class="blok-sizer"></div>
<div id="" class="block">
<div class="top">
<div class="center column"></div>
</div>
<div class="middle">
<div class="center column">
<span class="FooTitle">Overdracht</span>
<span class="Foo first transactiestatus">
<span class="FooName">Status</span>
<span class="FooValue">Beschikbaar</span>
</span>
<span class="Foo koopprijs">
<span class="FooName">Vraagprijs</span>
<span class="FooValue">€ 975.000,-</span>
</span>
</div>
</div>
</div>
</div>
</div>';
$dom = getDom($domcode);
$html = '';
$domxpath = new \DOMXPath($dom);
$newDom = new \DOMDocument;
$newDom->formatOutput = true;
$filtered = $domxpath->query("//div[#class='center column']/span");
foreach ($filtered as $myItem) {
$temp_name = $domxpath->evaluate("string(descendant::span[#class='FooName'])", $myItem);
echo "<br>";
echo strtolower(preg_replace('/\s*/', '', $temp_name));
echo " = ";
echo $domxpath->evaluate("string(descendant::span[#class='FooValue'])", $myItem);
}
Turns out I had been beating the wrong line of code all day. Apparently I needed to broaden the Filtered Search. If there's room for non-greedy code I'm all ears. Otherwise, I hope it helps somebody else.
$filtered = $domxpath->query("//div[#class='Fooen']/descendant::span");

PHP preg_match_all for new line

i need a data "2.5 (0.5)" and "3.5"
my pattern is '/class="match_total_goal_div">.+</s'
But it is not working.
Please help.
<div class="match_total_goal_div">
2.5 (0.5) </div>
<div class="match_half_goal_div hide" ">
</div>
</td>
<td class="text-center corner_goal_range">
<div>
<span class="newlabel">N.A.</span>
</div>
.
.
.
<div class="match_total_goal_div">
3.5 </div>
.
.
.
First, you need to add brackets around your .+ to capture the desired data. By the way you need a question mark: .+?.
Hope this can help you
$str = '<div class="match_total_goal_div">
2.5 (0.5) </div>
<div class="match_total_goal_div">
3.5 </div>';
$pattern = '/class="match_total_goal_div">(.+?)</s';
preg_match_all($pattern, $str, $matches);
var_dump($matches);
Check this code to accomplish your goal
<?php
$html = '<div class="match_total_goal_div">
2.5 (0.5) </div>
<div class="match_half_goal_div hide">
</div>
<td class="text-center corner_goal_range"></td>
<div>
<span class="newlabel">N.A.</span>
</div>
<div class="match_total_goal_div">
3.5 </div>';
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'match_total_goal_div';
$nodes = $finder->query("//*[contains(#class, '$classname')]");
foreach ($nodes as $node) {
echo $node->nodeValue."\n";
}
?>
Live demo : http://sandbox.onlinephpfunctions.com/code/b3e645ac56b9f7bf57d4519abd6b1be90ed87945

Find and replace html(Zend_Dom_Query+createElement()): Call to a member function createElement() on a non-object

I need to find and replace some html elements inside an html code (I followed this answer: Getting an element from PHP DOM and changing its value), to do so I retrieve the content with:
$transport = $observer->getTransport();
$html = $transport->getHtml();
$dom = new Zend_Dom_Query($html);
$document = $dom->getDocument();
and this is the result:
<div class="page-title category-title">
<h1>Title</h1>
</div>
<div class="category-products">
<div class="toolbar">
<div class="pager">
<p class="amount">Items 2 to 2 of 2 total</p>
<div class="limiter">
<label>Show</label>
<select onchange="setLocation(this.value)">
<option value="limit=1" selected="selected">1</option>
</select>per page</div>
<div class="pages"> <strong>Page:</strong>
<ol>
<li>
<a class="previous i-previous" href="p=1" title="Previous">
<img src="skin/frontend/default/default/images/pager_arrow_left.gif" alt="Previous" class="v-middle" />
</a>
</li>
<li>1
</li>
<li class="current">2</li>
</ol>
</div>
</div>
<div class="sorter">
<p class="view-mode">
<label>View as:</label> <strong title="Grid" class="grid">Grid</strong> List </p>
<div class="sort-by">
<label>Sort By</label>
<select onchange="setLocation(this.value)">
<option value="dir=asc&order=position" selected="selected">Position</option>
<option value="dir=asc&order=name">Name</option>
<option value="dir=asc&order=price">Price</option>
</select> <img src="skin/frontend/default/default/images/i_asc_arrow.gif" alt="Set Descending Direction" class="v-middle" />
</div>
</div>
</div>
<ul class="products-grid">
<li class="item first">
<a href="test/a-2.html" title="a" class="product-image">
<img src="media/catalog/product/cache/1/small_image/135x/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/small_image.jpg" width="135" height="135" alt="a" />
</a>
<h2 class="product-name">a</h2>
<div class="price-box"> <span class="regular-price" id="product-price-2">
<span class="price">$1.00</span> </span>
</div>
<div class="actions">
<button type="button" title="Add to Cart" class="button btn-cart" onclick="setLocation('test/a-2.html')"><span><span>Add to Cart</span></span>
</button>
<ul class="add-to-links">
<li>
Add to Wishlist
</li>
<li>
<span class="separator">|</span> Add to Compare
</li>
</ul>
</div>
</li>
</ul>
<script type="text/javascript">
decorateGeneric($$('ul.products-grid'), ['odd', 'even', 'first', 'last'])
</script>
<div class="toolbar-bottom">
<div class="toolbar">
<div class="pager">
<p class="amount">Items 2 to 2 of 2 total</p>
<div class="limiter">
<label>Show</label>
<select onchange="setLocation(this.value)">
<option value="limit=1" selected="selected">1</option>
</select>per page</div>
<div class="pages"> <strong>Page:</strong>
<ol>
<li>
<a class="previous i-previous" href="p=1" title="Previous">
<img src="skin/frontend/default/default/images/pager_arrow_left.gif" alt="Previous" class="v-middle" />
</a>
</li>
<li>1
</li>
<li class="current">2</li>
</ol>
</div>
</div>
<div class="sorter">
<p class="view-mode">
<label>View as:</label> <strong title="Grid" class="grid">Grid</strong> List </p>
<div class="sort-by">
<label>Sort By</label>
<select onchange="setLocation(this.value)">
<option value="dir=asc&order=position" selected="selected">Position</option>
<option value="dir=asc&order=name">Name</option>
<option value="dir=asc&order=price">Price</option>
</select> <img src="skin/frontend/default/default/images/i_asc_arrow.gif" alt="Set Descending Direction" class="v-middle" />
</div>
</div>
</div>
</div>
</div>
To find the lements I use Zend_Dom_Query:
$transport = $observer->getTransport();
$html = $transport->getHtml();
$dom = new Zend_Dom_Query($html);
$document = $dom->getDocument();
if(!is_object($document)){
Mage::log(print_r($document, TRUE), null, 'mylogfile1.log');
$transport->setHtml($html);
exit();
}
$node = $document->createElement("p", "This product isn't available in your country.");
Unfortunately it always exit in obeject check otherwise it returns this error:
Fatal error: Call to a member function createElement() on a non-object
EDIT
Full code, if anyone wants to see where I retrieve content (I have added some comments to be more clear):
//retrieve html from observer
$transport = $observer->getTransport();
$html = $transport->getHtml();
//Retrieve other info
$stored = json_decode(Mage::getStoreConfig('razorphyn/country/buttons'));
$theme=trim(Mage::getSingleton('core/design_package')->getTheme('frontend'));
$dom = new Zend_Dom_Query($html);
$document = $dom->getDocument();
//check if $document is an object
if(!is_object($document)){
Mage::log(print_r($document, TRUE), null, 'mylogfile1.log');
$transport->setHtml($html);
exit();
}
//Create node that will replace the finded ones
$node = $document->createElement("p", "This product isn't available in your country.");
$elArray=array();
$productsIds= array();
//Retrieve products id if button and store query results
if($stored[$theme]['isOnClick']){
$queryDom='button'.$stored[$theme]['class'].'[onclick*="/checkout/cart/add/"]';
$results = $dom->query($queryDom);
foreach ($results as $result) {
preg_match("/checkout\/cart\/add.+\/([0-9]+)\//",$result->getAttribute('onclick'),$currentProdId);
$elArray[$currentProdId[0]]=$result;
$productsIds[]=$currentProdId[0];
}
}
//Retrieve products id if form, runa nother query to find button and store query results
else{
$queryDom='form'.$stored[$theme]['formId'].'[action*="/checkout/cart/add/"]';
$results = $dom->query($queryDom);
foreach ($results as $result) {
preg_match("/checkout\/cart\/add.+\/([0-9]+)\//",$result->getAttribute('action'),$currentProdId);
if($currentProdId[0] && is_numeric($currentProdId[0])){
$productsIds[]=$currentProdId[0];
$formDOM = new Zend_Dom_Query($result);
$formButton = $dom->query('button'.$stored[$theme]['class']);
foreach($formButton as $child){
$elArray[$currentProdId[0]]=$child;
}
}
}
}
//Retrieve info from table
$collection = Mage::getModel('razorphyn_country/product')->getCollection()
->addFieldToFilter('active', 1)
->addAttributeToFilter('productId', array('in' => $productsIds));
$res = $collection->getFirstItem();
$country = Mage::getSingleton('core/session')->getCustomerCountry;
//Replace items
if(isset($res->allowed)){
foreach($collection as $res){
if(isset($res->allowed) && (($res->allowed==0 && strpos($res->country, $country) !== false) || ($res->allowed==1 && strpos($res->country, $country) === false))){
$document = $document->replaceChild($node,$elArray[$res->productId]);
}
}
}
//Return edited html
$html = $document->saveHTML();
$transport->setHtml($html);
You realise you are not selecting an element but creating one with createElement()?
I'm not sure where you want to place the paragraph, but let's say in the div.category-products. So let's try something like this;
$transport = $observer->getTransport();
$html = $transport->getHtml();
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//div[#class="category-products"]');
foreach($nodes as $node) {
$newNode = $dom->createElement("p", "This product isn't available in your country.");
$node->insertBefore($newNode, $node->nextSibling());
}

How to get the element in arrays

I'm working on my PHP to search for the elements. There are are one element called <p id='links'>, I use simple_html_dom method to parsing the contains from my script called get-listing.php.
Here is the example output from 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>
Here is my PHP script:
<?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">';
?>
I want to create the loops to get the url in each array from get-listing.php with one element id=links.
Can you please tell me how I can do that?
Assuming simple_html_dom.php gets your data as described here http://simplehtmldom.sourceforge.net/ then you should be able to use
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;
}
EDIT
if you want to sort through the hrefs you could do a few simple tests here
foreach ($links as $link) {
//Get raw URL's here
if (strstr($link->href,'get_listing')) {
$listings[] = $link->href;
} else {
$general[] = $link->href;
}
}

Categories