I am trying to echo out the href and the image src using getattribute but though the href gets echoed correctly I am unable to retrieve the image src...plz guide. below is my
html mockup
<div id="hot-deals">
<div class="all-deals">
<ul>
<li><a href="http://url1.com">
<img src="http://imagelink1.com"></a>
</li>
<li><a href="http://url2.com">
<img src="http://imagelink2.com"></a>
</li>
<li><a href="http://url3.com">
<img src="http://imagelink3.com"></a>
</li>
</ul>
</div>
</div>
my code
$nodes = $my_xpath->query( '//div[#id="hot-deals"]/div[#class="all-deals"]/ul/li/a' );
foreach( $nodes as $node )
{
$title=$node->getAttribute('href');
$img=$node->getAttribute('img/src');
echo $title.",".$img."<br>";
}
src is not attribute of a tag, so you need one more step to get inner img tag and then take its attribute
foreach( $nodes as $node ) {
$title = $node->getAttribute('href');
$imgTags = $node->getElementsByTagName('img');
$img = $imgTags->item(0)->getAttribute('src');
echo $title . "," . $img . "<br>";
}
You can try this code.
<?php
$str = '<div id="hot-deals">
<div class="all-deals">
<ul>
<li><a href="http://url1.com">
<img src="http://imagelink1.com"></a>
</li>
<li><a href="http://url2.com">
<img src="http://imagelink2.com"></a>
</li>
<li><a href="http://url3.com">
<img src="http://imagelink3.com"></a>
</li>
</ul>
</div>
</div>';
$nodes = simplexml_import_dom(DOMDocument::loadHTML($str))->xpath('//div[#id="hot-deals"]/div[#class="all-deals"]/ul/li/a');
foreach( $nodes as $node )
{
$title = $node['href'];
$src = $node->img['src'];
echo $title ." " . $src . '<br>';
}
Related
Simple HTML DOM find tags and fetch data from page link
Hi I'm Simple HTML DOM, basically i need to get h2 title and the content from
the links (page/id/1). The point I'm getting stack is getting data from page .
The format should be the same that is
Title
contet form lik1 ,
content from link5
title 2
content from link ,
content from 2
<section class="level">
<h2> title </h2>
<a class="links" href="page/id/1">link1 </a>
<a class="links" href="page/id/2">link2 </a>
<a class="links" href="page/id/3">link3 </a>
<a class="links" href="page/id/4">link4 </a>
<a class="links" href="page/id/5">link5 </a>
</section>
<section class="level">
<h2> title 2 </h2>
<a class="links" href="page/id/7">link1 </a>
<a class="links" href="page/id/8">link2 </a>
</section>
<section class="level">
<h2> title 3 </h2>
<a class="links" href="page/id/9">link2 </a>
<a class="links" href="page/id/10">link3 </a>
</section>
I know it should be along these line any help guys
foreach ($html->find('h2') as $key => $value) {
echo $html->find('h2',0)->plaintext;
//this is where Im stack getting the data from the link
foreach ( ) {
echo data from the link example.com/page.php/id/1
echo data from the link example.com/page.php/id/2
}
}
You could find the <section> with the classname level using find('section[class=level]') Then you could for example loop the childnodes and check the nodeName.
To get only the anchors, you could use find('section[class=level] a')
For example:
$html = new simple_html_dom();
$html->load($data);
$result = $html->find('section[class=level]');
foreach ($result as $item) {
foreach($item->childNodes() as $childNode) {
if ($childNode->nodeName() === "h2") {
echo $childNode->innertext . "<br>";
}
if ($childNode->nodeName() === "a") {
echo $childNode->getAttribute("href") . "<br>";
}
}
}
Result
title
page/id/1
page/id/2
page/id/3
page/id/4
page/id/5
title 2
page/id/7
page/id/8
title 3
page/id/9
page/id/10
I have to recover some news from a div of a site. The div is structured as follows:
The HTML Markup:
<ul id="news-accordion" class="rounded" style="padding: 2px;">
<li class="o">
<h3>
<span>TITLE ARTICLE</span>
<span>30/10/2014</span>
</h3>
<div style="display: none;">
<p>text of article</p>
</div>
</li>
<li class="e">
<h3>
<span>TITLE ARTICLE</span>
<span>28/10/2014</span>
</h3>
<div style="display: none;">
<p>text of article</p>
</div>
</li>
<li class="o">
<h3>
<span>TITLE ARTICLE</span>
<span>29/10/2014</span>
</h3>
<div style="display: none;">
<p>text of article</p>
</div>
</li>
</ul>
PHP
<?php
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('http://www.xxxxxxxxx/news.php'));
$news = $doc->getElementById('news-accordion');
$li = $news->getElementsByTagName('li');
foreach ($li as $row){
$title = $row->getElementsByTagName('h3');
echo $title->item(0)->nodeValue."<br><br>";
/*foreach ($title as $row2){
echo $row2->nodeValue."<br><br>";
//echo $row2->item(0)->nodeValue."<br><br>";
}*/
$text = $row->getElementsByTagName('p');
echo utf8_decode($text->item(0)->nodeValue)."<br><br><br>";
}
?>
The code works correctly, but when I print the contents of the span tag echo $title->item(0)->nodeValue;,
The text of the two span is printed together.
How can I take the contents of the two span separately? Thanks.
Yes you can, just adjust the ->item() index. Just like what you have done already in the other elements, point it to that header element, then just explicitly point it to those span children:
foreach ($li as $row){
$h3 = $row->getElementsByTagName('h3')->item(0);
$title = $h3->getElementsByTagName('span')->item(0); // first span
$date = $h3->getElementsByTagName('span')->item(1); // second span
echo $title->nodeValue . '<br/>';
echo $date->nodeValue . '<br/>';
$text = $row->getElementsByTagName('p');
echo utf8_decode($text->item(0)->nodeValue)."<br><br><br>";
}
$title = $row->getElementsByTagName('h3');
echo $title->item(0)->nodeValue."<br><br>";
Replace above two line with below (instead of using h3 tag use span tag)
$title = $row->getElementsByTagName('span');
echo $title->item(0)->nodeValue."<br><br>";
echo $title->item(1)->nodeValue."<br><br>";
It's working for me.
I need to extract data from HTML page which looks like:
<li>
<h2>
<span>rss</span>AC Ajaccio</h2>
<div class="club-left">
<img src="http://medias.lequipe.fr/logo-football/35/60?CCH-13-40" width="60" height="60">
</div>
<div class="club-right">
<ul class="club-links">
<li><span class="plus"></span>
Fiche club
</li>
<li><span class="plus"></span>
Calendrier
</li>
<li><span class="plus"></span>Effectif
</li>
<li><span class="plus"></span>
Stats joueurs
</li>
<li><span class="plus"></span>
Stats club
</li>
</ul>
</div>
<div class="clubt hidden">35</div>
<div class="clear"></div>
</li>
I would like to extract in PHP the href value and the text of this part:
**Stats joueurs**
I use the following code, but there is something missing:
$elements = $xpath->query("//div[#id='Base']/ul/li");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
if($node->nodeName!='#text'){
echo $node->nodeValue.";<br/>";
$stringData = trim($node->nodeValue).";";
}
}
}
UPDATE:
Try:
$elements = $xpath->query("//ul[#class='club-links']//a");
foreach ($elements as $element) {
echo $element->nodeValue." - ".$element->getAttribute("href")."<br/>";
}
I'am currently parsing in a twitter tweets by geocode and displaying them using php. But at the moment I'm unable to display the link to the tweet.
But within the XML it is feed->entry->
<link type="text/html" href="http://twitter.com/OFFICIALGRINZ/statuses/165838159928762368" rel="alternate" >
Is is possible to access the url from inside this tag?
Here is my code.
<?php
$tabTitle = ' | Twitter';
$pageIntroductionHeading = 'Twitter';
$pageIntroductionContent = 'Twitter';
$column1Heading = 'London';
$column2Heading = 'New York';
$column3Heading = 'Paris';
require_once 'header.php';
require_once 'navigationMenu.php';
?>
<?php require_once 'getWeather.php'; ?>
<?php require_once 'getExchangeRates.php'; ?>
<?php require_once 'pageIntroduction.php'; ?>
<div id="obsah" class="content box">
<div class="in">
<div class="shadow">
<img src="./img/threeCities.jpg" alt="Three Cities Banner" title="Three Cities" class="thumb" />
</div>
<ul class="columns">
<li class="col1">
<h3><?php
if (isset($column1Heading)) {
echo $column1Heading;
}
?></h3>
<p>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</p>
</li>
<li class="col2">
</li>
</ul>
<div class="clear"></div>
</div>
<?php require_once 'footer.php'; ?>
You could use DomDocument Parser and DomXPath to access your link.
$dom = new DomDocument();
$dom->load('yourfeed.xml');
$xpath = new DomXPath($dom);
$links = $xpath->query('/feed/entry/link'); // returns a DomNodeList with 0 or more elements
foreach ($links as $link)
{
// $link->getAttribute('href');
}
This code is untested.
This is my first post, so I will be trying to be as thorough as possible. I am also very new to PHP.
This is a wordpress site using PODS CMS plugin so keep in mind the wordpress image uploader allows access to multiple sizes of one singular image upload. The concept is that I have a group of data called "team" and this group has three fields - images, title, bio. I will be generating a list of thumbnails for each team member in one containing unordered list and then in another containing div I will have a larger version of he image, the title, and the bio. I am basically making a tabbed content area where the thumbnails are the tabs
The ultimate HTML output would be:
<ul>
<li> <img src="thumbnailurl.jpg"/></li>
<li> <img src="thumbnailurl2.jpg"/></li>
<li> <img src="thumbnailurl3.jpg"/></li>
</ul>
<div class="panes">
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
</div>
The current issue I am having is that I can get all of the image urls for the first record, but when it comes to the second record in the second foreach i need to re-run the array for the new record but I can not figure out how.
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
$mylist=array();
while ($Record->fetchRecord())
{
$image_array = $Record->get_field('photo');
$title = $Record->get_field('name');
$desc = $Record->get_field('bio');
$id = $Record->get_field('id');
$mylist[] = array('name' => $title, 'desc' => $desc, 'id'=> $id );
?>
<ul>
<?php
foreach($image_array as $i => $image)
{
$image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
$image_thumb_url = $image_thumb_url[0];
$image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
$image_med_url = $image_med_url[0];
$image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
$image_large_url = $image_large_url[0];
$image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
$image_full_url = $image_full_url[0];
?>
<li>
<a href="<?php echo $image_large_url; ?>">
<img src="<?php echo $image_thumb_url; ?>" />
</a>
</li>
<?php } ?>
<?php } ?>
</ul>
<div class="panes">
<?php
foreach ($mylist as $person)
{ ?>
<div class="team-member" id="member<?php echo $person['id']; ?>">
<h2><?php echo $person['name']; ?></h2>
<?php echo $person['desc']; ?>
<a href="<?php echo $person['photo']; ?>">
<img src="<?php echo $person['photo']; ?>" />
</a>
<?php } ?>
</div>
</div>
Okkkay.. So i have the first problem solved!!! But it brings up a second one. I am thinking I will either need a second image field OR call just the first image in the array in the <li> and just the second image in the array for the <div>:
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
$mylist=array();
$cnt = 0;
?>
<ul class="tabs">
<?php
while ($Record->fetchRecord()) :
$mylist[$cnt] = array(
'name' => $Record->get_field('name'),
'desc' => $Record->get_field('bio'),
'id'=> $Record->get_field('id')
);
?>
<?php
$image_array = $Record->get_field('photo');
foreach($image_array as $i => $image) :
$image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
$mylist[$cnt]['img_thumb'] = $image_thumb_url[0];
$image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
$mylist[$cnt]['img_med'] = $image_med_url[0];
$image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
$mylist[$cnt]['img_large'] = $image_large_url[0];
$image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
$mylist[$cnt]['img_full'] = $image_full_url[0];
?>
<li>
<a href="#">
<img src="<?php echo $image_thumb_url[0]; ?>" />
</a>
</li>
<?php endforeach; ?>
<?php
$cnt++;
endwhile;
?> </ul>
<div class="panes">
<?php foreach ($mylist as $person) : ?>
<div class="team-member" id="member<?php echo $person['id']; ?>"><div id="member-info"><h2>Meet <?php echo $person['name']; ?></h2>
<?php echo $person['desc']; ?></div>
<a href="<?php echo $person['img_large']; ?>" rel="prettyPhoto">
<img src="<?php echo $person['img_med']; ?>" style="float:left" />
</a>
</div>
<?php endforeach; ?>
</div>
I think I can see what your problem is. Your second foreach loop doesn't benefit from the while loop, as such your only option if following this path would be to reloop through the fetched data. I wouldn't recommend doing this as there is nearly always a way you can write the code needed for the second loop into the original loop.
As an example of this I've re-written your code to incorporate the second foreach loop into the while loop (negating its need). This way, every record has a new entry into $html and $html2. Once it's looped through you have two variables that you can concat to produce the full html you are looking for.
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
// Custom variables
$html = "<ul>\n";
$html2 = "";
$image_type=array('thumbnail','medium','large','full');
while ($Record->fetchRecord()) {
$image_array = $Record->get_field('photo');
$title = $Record->get_field('name');
$desc = $Record->get_field('bio');
$id = $Record->get_field('id');
foreach($image_array as $i => $image) {
foreach ($image_type as $type) {
$image_temp = wp_get_attachment_image_src( $image['ID'], $type , false );
$image_url[$type] = $image_temp[0];
} # End of Foreach loop
// Create List HTML in primary output variable
$html .= "<li>\n".
"<a href=\"" . $image_url['large'] ."\">\n".
"<img src=\"" . $image_url['thumbnail'] ."\" />\n".
"</a>\n".
"</li>\n";
} #End of foreach loop
// Create User HTML in secondary output variable
$html2 .= "\t<div class=\"team-member\" id=\"member" . $id . ">\n".
"<h2>" . $title . "</h2>\n".
$desc . "\n".
"<a href=\"" . $photo . ">\n". # $photo is not declared in this code (will error)
"<img src=\"" . $photo . " />\n". # as above comment
"</a>\n";
"</div>\n";
} # End of while loop
// Wrap up the list elements, concat the secondary HTML.
$html .= "</ul>\n\n".
"<div class=\"panes\">\n".
$html2 . "\n".
"</div>\n";
echo $html;
?>