brad45429 Posted February 7, 2007 Share Posted February 7, 2007 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! Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 7, 2007 Share Posted February 7, 2007 Hello brad45429, Use the following additional line: <?php $content = substr($content, 0, 50); //source string, starting point, how many ?> Hope this helps Quote Link to comment Share on other sites More sharing options...
brad45429 Posted February 7, 2007 Author Share Posted February 7, 2007 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/ Quote Link to comment Share on other sites More sharing options...
brad45429 Posted February 7, 2007 Author Share Posted February 7, 2007 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! Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 7, 2007 Share Posted February 7, 2007 very nice Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 7, 2007 Share Posted February 7, 2007 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. Quote Link to comment Share on other sites More sharing options...
brad45429 Posted February 7, 2007 Author Share Posted February 7, 2007 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 Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 7, 2007 Share Posted February 7, 2007 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. Quote Link to comment Share on other sites More sharing options...
brad45429 Posted February 8, 2007 Author Share Posted February 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 8, 2007 Share Posted February 8, 2007 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"; ?> Quote Link to comment Share on other sites More sharing options...
brad45429 Posted February 8, 2007 Author Share Posted February 8, 2007 Copied and pasted it in, now I get... Fatal error: Call to undefined function: stripos() in /home/www/xenianaz/blogs/index.php on line 258 Quote Link to comment Share on other sites More sharing options...
brad45429 Posted February 8, 2007 Author Share Posted February 8, 2007 Sorry - Line 258 is this line... <?php $spacePosition = stripos($remainingContent, " "); //position of next space/word break ?> Quote Link to comment Share on other sites More sharing options...
brad45429 Posted February 8, 2007 Author Share Posted February 8, 2007 GOT IT! I learned from here... http://codewalkers.com/archives/phpcoding/10127.html That stripos does not exist in PHP4, ( I am using PHP 4.4.2 ) So I added the code there and NOW IT WORKS! THANKS AGAIN! Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 8, 2007 Share Posted February 8, 2007 sweet. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.