Page 1 of 1
PHP/XML Printer Friendly
Posted: Fri Dec 19, 2008 6:25 pm
by Harry190091
Hello,
I'm parseing XML/RSS onto my page using php. Working fine so far, but i need help with one part.
the link in the RSS to the article is
http://www.medicalnewstoday.com/articles/133241.php
But what i would like to do is instead of pulling out that link, i would like to pull out the printer friendly link which is contained in the article, so for this page i would like to display
http://www.medicalnewstoday.com/printer ... sid=133241
How would i go about modifying my code for this ?
Code: Select all
$rss_document = simpleXML_load_file('http://www.medicalnewstoday.com/rss/allergy.xml');
/Loop through each item in document
foreach($RSS_doc->channel->item as $RSS_item) {
//The variable $RSS_item will hold a different item for each loop
//Write the current item's title to the screen in HTML tags
echo "<h3>$RSS_item->title</h3>";
echo "<a href='".$RSS_item->link."'>{$RSS_item->link}</a><br />";
echo "<b>$RSS_item->pubDate</b><br />";
echo "$RSS_item->description<br /><br />";
Re: PHP/XML Printer Friendly
Posted: Fri Dec 19, 2008 6:47 pm
by requinix
Seeing as how the RSS feed only gives the normal webpage version, you'll have to make an assumption.
Assume that the link given in the RSS will stay in the same format, grab the ID number from it, and use that to form the new link.
Code: Select all
foreach($RSS_doc->channel->item as $RSS_item) {
//The variable $RSS_item will hold a different item for each loop
//Write the current item's title to the screen in HTML tags
echo "<h3>$RSS_item->title</h3>";
$url = "http://www.medicalnewstoday.com/printerfriendlynews.php?newsid=" . substr("$RSS_item->link", 41, -4);
echo "<a href='$url'>$url</a><br />";
echo "<b>$RSS_item->pubDate</b><br />";
echo "$RSS_item->description<br /><br />";
}
Re: PHP/XML Printer Friendly
Posted: Sat Dec 20, 2008 1:00 am
by Harry190091
tasairis wrote:Seeing as how the RSS feed only gives the normal webpage version, you'll have to make an assumption.
Assume that the link given in the RSS will stay in the same format, grab the ID number from it, and use that to form the new link.
Code: Select all
foreach($RSS_doc->channel->item as $RSS_item) {
//The variable $RSS_item will hold a different item for each loop
//Write the current item's title to the screen in HTML tags
echo "<h3>$RSS_item->title</h3>";
$url = "http://www.medicalnewstoday.com/printerfriendlynews.php?newsid=" . substr("$RSS_item->link", 41, -4);
echo "<a href='$url'>$url</a><br />";
echo "<b>$RSS_item->pubDate</b><br />";
echo "$RSS_item->description<br /><br />";
}
thanks. What does the 41, -4 mean please ?
Re: PHP/XML Printer Friendly
Posted: Sat Dec 20, 2008 1:28 am
by RobertGonzalez
Since there is no mention of a printer friendly link in the RSS but you do have the link in the RSS, you could do a simple string replacement. You want to make
http://www.medicalnewstoday.com/articles/133241.php
look like
http://www.medicalnewstoday.com/printer ... sid=133241
Then you could just str_replace the link, like:
Code: Select all
<?php
$printer = str_replace(array('.php', 'articles/'), array('', 'printerfriendlynews.php?newsid='), $RSS_item->link);
echo $printer;
?>
Not the most elegant but very effective.
Re: PHP/XML Printer Friendly
Posted: Sat Dec 20, 2008 2:23 am
by requinix
Harry190091 wrote:thanks. What does the 41, -4 mean please ?
41 is the position where the numbers begin. Go ahead and count (remember that the first character is 0, not 1).
The third argument is the length of the string to capture: -X means the rest of the string except the last X characters.
Re: PHP/XML Printer Friendly
Posted: Sat Dec 20, 2008 4:58 am
by Harry190091
hi, thanks. This is not displaying anything. Before it showed the articles/13341.php link, but now it does not show up.
Re: PHP/XML Printer Friendly
Posted: Sat Dec 20, 2008 5:02 am
by Harry190091
tasairis wrote:Harry190091 wrote:thanks. What does the 41, -4 mean please ?
41 is the position where the numbers begin. Go ahead and count (remember that the first character is 0, not 1).
The third argument is the length of the string to capture: -X means the rest of the string except the last X characters.
From your code help, it is almost displaying correctly, but not picking up the newsid number, It is displaying this below.
http://www.medicalnewstoday.com/printer ... hp?newsid=
Re: PHP/XML Printer Friendly
Posted: Sat Dec 20, 2008 6:17 pm
by Harry190091
I'm still struggling with this, just cannot figure it out.
This code is not displaying a link at all.
Code: Select all
<?php
$printer = str_replace(array('.php', 'articles/'), array('', 'printerfriendlynews.php?newsid='), $RSS_item->link);
echo $printer;
?>
This code is only displaying
http://www.medicalnewstoday.com/printer ... hp?newsid=
So not picking up the article number.
Code: Select all
$url = "http://www.medicalnewstoday.com/printerfriendlynews.php?newsid=" . substr("$RSS_item->link", 41, -4);
echo "<a href='$url'>$url</a><br />";
Re: PHP/XML Printer Friendly
Posted: Sun Dec 21, 2008 9:15 am
by RobertGonzalez
Did you try my suggestion at all? This is the code I just tested:
Code: Select all
<?php
$rss = simpleXML_load_file('http://www.medicalnewstoday.com/rss/allergy.xml');
foreach($rss->channel->item as $item) {
echo "<h3>$item->title</h3>";
echo "<a href='".$item->link."'>$item->link</a><br />";
echo str_replace(array('.php', 'articles/'), array('', 'printerfriendlynews.php?newsid='), $item->link) . '<br />';
echo "<b>$item->pubDate</b><br />";
echo "$item->description<br /><br />";
}
And this is its output first segment:
Code: Select all
Anti-Allergy Vaccines To Help People With Asthma
http://www.medicalnewstoday.com/articles/133241.php
http://www.medicalnewstoday.com/printer ... sid=133241
Wed, 17 Dec 2008 03:00:00 PST
Research funded by Asthma UK has led to the development of a vaccine treatment that can prevent asthma-like symptoms in mice. Dr Noble and his team at King's College London have been studying allergic mechanisms in mice and investigating whether it is possible to regulate the immune system's response to potential allergic triggers.
Is the second url not what you are after?
Re: PHP/XML Printer Friendly
Posted: Sun Dec 21, 2008 10:19 am
by Harry190091
Everah wrote:Did you try my suggestion at all? This is the code I just tested:
Code: Select all
<?php
$rss = simpleXML_load_file('http://www.medicalnewstoday.com/rss/allergy.xml');
foreach($rss->channel->item as $item) {
echo "<h3>$item->title</h3>";
echo "<a href='".$item->link."'>$item->link</a><br />";
echo str_replace(array('.php', 'articles/'), array('', 'printerfriendlynews.php?newsid='), $item->link) . '<br />';
echo "<b>$item->pubDate</b><br />";
echo "$item->description<br /><br />";
}
And this is its output first segment:
Code: Select all
Anti-Allergy Vaccines To Help People With Asthma
http://www.medicalnewstoday.com/articles/133241.php
http://www.medicalnewstoday.com/printer ... sid=133241
Wed, 17 Dec 2008 03:00:00 PST
Research funded by Asthma UK has led to the development of a vaccine treatment that can prevent asthma-like symptoms in mice. Dr Noble and his team at King's College London have been studying allergic mechanisms in mice and investigating whether it is possible to regulate the immune system's response to potential allergic triggers.
Is the second url not what you are after?
yes the 2nd link is what i am after. I would like to only show that as a clickable link, and not show the /articles/ link at all.
Re: PHP/XML Printer Friendly
Posted: Sun Dec 21, 2008 3:18 pm
by RobertGonzalez
The code I posted gives you everything you need to get what you want. Look at it closely and see if you can't figure out how to get at what you want.
Re: PHP/XML Printer Friendly
Posted: Mon Dec 22, 2008 11:23 am
by Harry190091
Everah wrote:The code I posted gives you everything you need to get what you want. Look at it closely and see if you can't figure out how to get at what you want.
My fault , I was sticking the $printer = string outside the loop. Now i have it inside the loop it is working fine.
many thanks