The script I am using to pull in the feed titles is:
<?php
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a> </li>";
$i++;
if($i==5) break;
}
echo "</ul>";
}
?>
I would like to pull in the images for each title/article and place them next to the title.
The float part is easy. I'm having trouble getting the actual image in. I've tried that mark like this: <img src="$entry->image" />, but it didn't work.
How would I go about this please?
As the answer to random's suggestion:
I'm using $item['description'] to represent the description string.
Then match for img tag
Output image tag
Then remove the image tag from the original $content and output that, you're left with just the description text
<?php
$content = $item['description'];
preg_match('/(<img[^>]+>)/i', $content, $matches);
echo $matches[0];
echo str_replace($matches[0],"",$content);
?>
When displaying the feed contents, variables such as $entry->link and $entry->title work because they are valid, present and required elements in a standard RSS feed item.
To call $entry->image would require the source RSS to use and populate that optional element.
Some feeds may include that data, but most will not.
Alternatively, you can write your own function or method to scan the contents of the description element and find any suitable image to include as you need.
Related
I have a Wordpress site that I am building for a client and one custom post type field will allow users to enter a link and then the text for the link in a format as pictured below and called resources.
That info them needs to be output in an anchor tag as a <li>. I am newish to php and this is what I have so far for code
<ul>
<?php
$rawcontent = get_field("resources");
$rawcontent = preg_replace("!</?p[^>]*>!", "", $rawcontent);
$all_links = preg_split("/(\n)/", $rawcontent);
$firstpart = array_pop(explode(',', $rawcontent));
foreach($all_links as $link) {
if(!trim($link)) continue;
echo "<li><a href='$link'>$firstpart</a></li>";
}
?>
</ul>
when I print $rawcontent (resources) before any of my code executes is apperas as:
www.mylink1.com,link copy 1
www.mylink2.com, link copy 2
www.mylink3.com,link copy 3
with the code I have implemented now it comes out as
How can I get this to return just the link for the href and the just the link copy part for the anchor text for each anchor tag?
I think this will do it.
I first explode on new line just like you do, then I foreach the lines.
When I foreach the lines I explode the line on comma.
Now I have an array with link as first item, and text as the second item.
$str = "www.mylink1.com,link copy 1
www.mylink2.com, link copy 2
www.mylink3.com,link copy 3";
$lines = explode(PHP_EOL, $str);
Foreach($lines as $line){
$linktext = explode(",", $line);
Echo "<li><a href='$linktext[0]'>$linktext[1]</a></li>";
}
https://3v4l.org/9DEoo
I see that your link2 has a space in the text.
You can remove that with trim when you echo.
Echo "<li><a href='" . trim($linktext[0]) . "'>" . trim($linktext[1]) . "</a></li>\n";
I added trim on both link and text, it can be good to have. Just in case...
https://3v4l.org/6RkW3
say i have html code like this
$html = "This is some stuff right here. OH MY GOSH";
i am trying to get values of href and also on which anchor work i mean check this out text i am able to get href value by following this code
$displaybody->find('a ') as $element;
echo $element;
well it works for me but how do i get value of check this out could you guys help me out. i did search but i am not able to find it out . thanks in advance
my actual html look like this
» Download MP4 « - <b>144p (Video Only)</b> - <span> 19.1</span> MB<br />
my href look like this above code return download mp4 and i want it like downloadmp4 114p (video only) 19.1 mb how do i do that
If what you are using now is the SimpleHTMLDOM, then ->innertext works fine on that anchor elements that you have found:
include 'simple_html_dom.php';
$html = "This is some stuff right here. OH MY GOSH";
$displaybody = str_get_html($html);
foreach($displaybody->find('a ') as $element) {
echo $element->innertext . '<br/>';
}
If you were referring to PHP's DOMDocument, then its not find() function you need to use, to target each anchor element, you need to use ->getElementsByTagName(), then each selected elements you need to use ->nodeValue:
$html = "This is some stuff right here. OH MY GOSH";
$dom = new DOMDocument();
$dom->loadHTML($html);
foreach($dom->getElementsByTagName('a') as $element) {
echo $element->nodeValue . '<br/>';
}
As I want to understand Simple HTML Dom a bit I am playing around with it, to test options on my localhost.
Basically I want to take the titles and intro's of this website and display them on my page.
The title as <h2> and the intro as <p>.
What am I doing wrong?
<?php
include 'simple_html_dom.php';
// Create DOM from URL
$html = file_get_html('http://www.nu.nl/algemeen');
foreach($html->find('div[class=list-overlay]') as $article){
$title['intro'] = $article->find('span[class=title]', 0)->innertext;
$intro['details'] = $article->find('span[class=excerpt]', 0)->innertext;
echo '<h2>'. $articles . '</h2>
<p>'. $title .'</p>';
}
?>
edit: There was a double line in there.
Your soulution is somehow right. You have only few typos in variable names. Here is my editation of your code. Also I have added few comments to help you understand.
<?php
include 'simple_html_dom.php';
// Create DOM from URL
$html = file_get_html('http://www.nu.nl/algemeen');
// exctract all elements matching selector div[class=...]
foreach($html->find('div[class=list-overlay]') as $article){
// and for each extract first (0) element that matches to span[class=title]
$title = $article->find('span[class=title]', 0)->innertext;
// and do the same for intro, extract first element that belongs to selector
$intro = $article->find('span[class=excerpt]', 0)->innertext;
// and write it down simply
echo '<h2>'. $title . '</h2>';
echo '<p>' . $intro . '</p>';
}
?>
This solution isn't good though. The have bad structure of their HTML so it is not easy to select only articles, because they don't have them in div with ID articles (for example. You are lucky man anyway, because they provide you XML feed of their articles that is much easier to parse (also less data to transfer and so on). You can find it here and extract titles and intros for your website.
I'm using Simple HTML DOM to try and extract a div and all of it's contents from a target URL, here is my code:
<?php
require 'simple_html_dom.php';
$html = file_get_html('http://mozilla.org');
foreach($html->find('.accordion') as $element)
echo $element . '<br>';
?>
The problem I have is that the above code only extracts the plain text of the div. There are also images in the div that I need to extract. If I use this following code, then all images are extracted but so is everything else in the page.
<?php
require 'simple_html_dom.php';
$html = file_get_html('http://mozilla.org');
echo $html;
?>
So my question is, how can I use the first bit of code to extract the contents + images from .accordion?
Thanks
You could always try;
$imgs = array();
foreach($html->find('.accordion',0)->find('img') as $img){
$imgs[] = $img->src;
}
print_r($imgs);
This should populate the $imgs variable with all of the image links from the .accordion div.
:)
I'm trying to set up a php page made up of images from 3 different RSS feeds, in 3 columns. The first feed is from a Wordpress blog, the second from an Etsy store, and the third from a Flickr feed.
What I would like to do is grab the link to the images from the first 3 items in each feed.
Relevant information:
The Wordpress feed puts the link in the description, which is formatted as CDATA.
The Etsy feed puts the image link as the first item in the description (E.g. <img src="http://ny-image2.etsy.com/il_155x125.126568958.jpg" />), but also has it in a <g:image_link> tag.
The Flickr feed has the link in the description, but it comes third after links for the user's profile and a link to the photo page. There is also <media:content url="http://farm5.static.flickr.com/4016/4377189674_d6f3aafa81_m.jpg"
type="image/jpeg"
height="159"
width="240"/>.
I've read over the basics of both php and simplexml but it seems what I'd like to do is too complicated for me to figure out on my own. I'd prefer to have a separate functions.php file so I only have to call the function on the web page.
ETA:
I've got the images for the Etsy feed pulled using
<?php
function getEtsyFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "
<li>
<a href='$entry->link'><img src=" . $entry->children('g', true)->image_link . " /></a>
</li>";
}
echo "</ul>";
}
?>
and the images from the Flickr feed pulled using
<?php
function getFlickrFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "
<li>
<a href='$entry->link'><img src=" . $entry->children('media', true)->thumbnail->attributes()->url . " /></a>
</li>";
}
echo "</ul>";
}
?>
I'm still not sure what to do about the blog feed, or how to only show the first 3 images without breaking the code that I do have.
You should try MagPie.
http://magpierss.sourceforge.net/