PHP/XML Printer Friendly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Harry190091
Forum Newbie
Posts: 9
Joined: Fri Dec 19, 2008 6:21 pm

PHP/XML Printer Friendly

Post 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 />";
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP/XML Printer Friendly

Post 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 />";
}
Harry190091
Forum Newbie
Posts: 9
Joined: Fri Dec 19, 2008 6:21 pm

Re: PHP/XML Printer Friendly

Post 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 ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP/XML Printer Friendly

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP/XML Printer Friendly

Post 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.
Harry190091
Forum Newbie
Posts: 9
Joined: Fri Dec 19, 2008 6:21 pm

Re: PHP/XML Printer Friendly

Post by Harry190091 »

Everah wrote: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.
hi, thanks. This is not displaying anything. Before it showed the articles/13341.php link, but now it does not show up.
Harry190091
Forum Newbie
Posts: 9
Joined: Fri Dec 19, 2008 6:21 pm

Re: PHP/XML Printer Friendly

Post 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=
Harry190091
Forum Newbie
Posts: 9
Joined: Fri Dec 19, 2008 6:21 pm

Re: PHP/XML Printer Friendly

Post 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 />";
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP/XML Printer Friendly

Post 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?
Harry190091
Forum Newbie
Posts: 9
Joined: Fri Dec 19, 2008 6:21 pm

Re: PHP/XML Printer Friendly

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP/XML Printer Friendly

Post 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.
Harry190091
Forum Newbie
Posts: 9
Joined: Fri Dec 19, 2008 6:21 pm

Re: PHP/XML Printer Friendly

Post 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
Post Reply