Jump to content

[SOLVED] RSS Parsing.....


brad45429

Recommended Posts

Hello,

 

I am using this PHP script "Magpie RSS" to parse my atom.xml feed to be able to display the most recent post from a blog on a seperate page.  The parsing is working HOWEVER - I want to be able to limit the length of the displayed post to only about 50 characters or so and have a link to the entire post.  I DO NOT know the code to limit the $content though.  Can anyone help?  Here is what the page looks like.... http://www.xenianaz.org/blogs/index.php

 

Here is my php code....

 

<?php
define('MAGPIE_DIR', '../blogs/magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');

$rss = fetch_rss('http://xenianaz.org/blogs/jim/atom.xml');

$item = $rss->items[0];
$content = $item['atom_content'];
echo "<p>Latest Blog Entry:<br>$content</p>\n";
?> 

 

THANKS!

Link to comment
Share on other sites

Ultrus,

 

Thank You.  I have added the additional line to include the line you recommended to show as follows:

 

<?php
define('MAGPIE_DIR', '../blogs/magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');

$rss = fetch_rss('http://xenianaz.org/blogs/jim/atom.xml');

$item = $rss->items[0];
$content = substr($content, 0, 50); //source string, starting point, how many
$content = $item['atom_content'];
echo "<p>Latest Blog Entry:<br>$content</p>\n";
?> 

 

Did I get it right?  However it still shows the ENTIRE most recent post.  I wonder if it is something to do with the files associated with Magpie?  These are the files...  http://www.xenianaz.org/blogs/magpie/

Link to comment
Share on other sites

Ultrus - IT WORKS!

 

I moved the location of the additional line and it works now.  (I used 200 instead of 50)

 

<?php
define('MAGPIE_DIR', '../blogs/magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');

$rss = fetch_rss('http://xenianaz.org/blogs/jim/atom.xml');

$item = $rss->items[0];
$content = $item['atom_content'];
$content = substr($content, 0, 200); //source string, starting point, how many
echo "<p>Latest Blog Entry:<br>$content...</p>\n";
?> 

 

I also added a ... at the end of $content to display ... at the end of the breakoff.  Here is a question though.  It is cutting off in the middle of words.  Is there anyway to have it cut off after a word or at the end of a sentance even?  This may not be possible I realize.

 

Here is what it looks like currently (I have not formatted the page yet with graphics or anything)

 

http://www.xenianaz.org/blogs/index.php

 

THANKS!

Link to comment
Share on other sites

uggh. my brain quit working. Perhaps find a " " after 200 character, perhaps create a substring like the line I sent you of 200 to  ???

 

$addedContent = substr($content, 200, (strlen($content) - 200));

 

//get next space location - sorry, can't think of what to write on this

$addedContent = substr($addedContent , 0, */ number to next space */);

 

$content .= $addedContent; //append the remaining of the last word

 

I know all of the above is probably jiberish. I will take a fresh look at it tomorrow.

 

Good night.

 

 

Link to comment
Share on other sites

Ultrus,

 

Thanks SO MUCH for all your help.  It is jiberish to me.  (then again it always is until it works!)

 

I will try this substring later tonight and see if it works.

 

Thank God for people like you helping out the little people of the world!

 

Take Care,

 

Brad

Link to comment
Share on other sites

Ok. I'm back with a brain. Here is how I would limit your content sample to 200 characters, plus the remaining of any word that might have been chopped off:

 

<?php

//this changed a bit. It's not $content anymore
$allContent = $item['atom_content'];

//first 200 characters, some word might be chopped off here
$content = substr($allContent, 0, 200);

//whatever is being hidden, with possible chopped in half word at beginning
$remainingContent = substr($allContent , 200, (strlen($allContent) - 200));

//might want to check if a space is the last character of the current content
//if so, no words were chopped in half
if(substr($content, (strlen($content) - 1), 1)) {
     $remainingContent = false;
}

//if there were less than 200 characters to begin with, skip the following
if($remainingContent && strlen($remainingContent) > 0) {
     $spacePosition = stripos($remainingContent, " "); //position of next space/word break
     if($spacePosition)
          $content .= substr($remainingContent, 0, ($spacePosition + 1));
     } else {
          $content .= $remainingContent;
     }
}

?>

 

That should do it. I have not tested it for errors so let me know if there are issues. :)

Link to comment
Share on other sites

Thanks Ultrus,

 

$remainingContent does not seem to be working though.  Thanks for all your hard work though.

 

Please make sure I have the echo line correct.  I tried $allContent which worked and $content by itself - both work.  $remainingContent does not work by itself or with $content.

 

<?php
define('MAGPIE_DIR', '../blogs/magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');

$rss = fetch_rss('http://xenianaz.org/blogs/jim/atom.xml');
$item = $rss->items[0];
//this changed a bit. It's not $content anymore
$allContent = $item['atom_content'];
//first 200 characters, some word might be chopped off here
$content = substr($allContent, 0, 200);
//whatever is being hidden, with possible chopped in half word at beginning
$remainingContent = substr($allContent , 200, (strlen($allContent) - 200));
//might want to check if a space is the last character of the current content
//if so, no words were chopped in half
if(substr($content, (strlen($content) - 1), 1)) {
     $remainingContent = false;
}
//if there were less than 200 characters to begin with, skip the following
if($remainingContent && strlen($remainingContent) > 0) {
     $spacePosition = stripos($remainingContent, " "); //position of next space/word break
     if($spacePosition)
          $content .= substr($remainingContent, 0, ($spacePosition + 1));
     } else {
          $content .= $remainingContent;
    }
echo "<p>Latest Blog Entry:<br>$content$remainingContent</p>\n";
?> 

 

http://www.xenianaz.org/blogs/index.php

 

THANKS A BILLION.

Link to comment
Share on other sites

ah, I see a mistake. Try this and see if it works:

 

<?php
define('MAGPIE_DIR', '../blogs/magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');

$rss = fetch_rss('http://xenianaz.org/blogs/jim/atom.xml');
$item = $rss->items[0];

//this changed a bit. It's not $content anymore
$allContent = $item['atom_content'];

//first 200 characters, some word might be chopped off here
$content = substr($allContent, 0, 200);

//whatever is being hidden, with possible chopped in half word at beginning
$remainingContent = substr($allContent , 200, (strlen($allContent) - 200));

//might want to check if a space is the last character of the current content
//if so, no words were chopped in half
if(substr($content, (strlen($content) - 1), 1) == " ") { //this is where I made my mistake
     $remainingContent = false;
}

//if there were less than 200 characters to begin with, skip the following
if($remainingContent && strlen($remainingContent) > 0) {
     $spacePosition = stripos($remainingContent, " "); //position of next space/word break

     if($spacePosition)
          $content .= substr($remainingContent, 0, ($spacePosition + 1));
     } else {
          $content .= $remainingContent;
    }

echo "<p>Latest Blog Entry:<br>$content</p>\n";
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.