Jump to content

truncate a RSS-feed with PHP string functions like substr


Maze

Recommended Posts

this is a RSS-question: Im pulling an RSS feed from a site to display on my homepage. It displays the titles correctly, but I want
to truncate the number of characters per post so it doenst take up a long section if the title is long. How do I do that? here is the code Im using


<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');

$rss = fetch_feed('http://examplesite.com/rss');
if (!is_wp_error( $rss ) ) :

    $maxitems = $rss->get_item_quantity(5);

    $rss_items = $rss->get_items(0, $maxitems);
endif;
?>

<ul>
    <?php if ($maxitems == 0) echo '<li>No items.</li>';
    else

    foreach ( $rss_items as $item ) : ?>
    <li>
        <a href='<?php echo $item->get_permalink(); ?>'
        title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
        <?php echo $item->get_title(); ?></a>
    </li>
    <?php endforeach; ?>
</ul>


well I am trying to limit the number of words displayed in the excerpt Basicly, the problem is that I want to use the
same excerpt in two different loops. The first one will display the whole excerpt, the second loop will only display a smaller
part of it. Therefore I cannot limit the words number of ALL the excerpts, but I would need to do that locally.
Ideally, if there is a solution to this I can use the same excerpts in many different places on
the blog, using the same excerpt but in its longer/shorter version depending on the situation.


 

<?php $little_excerpt = substr(the_excerpt(),0,XY); ?>

doesn’t work because the_excerpt() is not at string.

well we can Use get_the_excerpt() instead, that will do the trick. However, this is still limiting characters, not words.


I just found a solution to limiting the number of words in the excerpt without plugins. Put the following piece of code in your templates functions.php file:

<?php
function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words);
}
?>

Next, put the following piece of code in your template where you want to display the excerpt:
 

<?php
  $excerpt = get_the_excerpt();
  echo string_limit_words($excerpt,25);
?>

Where 25 is the number of words to display.
php




    
we can use PHP string functions like substr to return a number of characters rather than word count.

The post content aaaaaaaaaaaaaaa aaaaaaaaaaaaa will be much longer than a a.
 

$max_length = 150;

echo substr( $string, 0, $max_length );

As a function it might look like:
 

function string_limit_words( $string = '', $count = 25, $after = '...' ) {
    if ( strlen( $string ) <= $count ) {
        return $string;
    }

    return substr( $string, 0, $count ) . $after;
}

echo string_limit_words( "This is a lot of words", 10 );

// This is a ...

the_excerpt() prints the output while get_the_excerpt() will return the string to allow manipulation before output.

 

what do you say?

 

look forward to hear from you
 

Link to comment
Share on other sites

I'm not going to read through your entire post. But, based on what I understand, I would create a function to output the limited string. Then when you need to output the title either use the title directly (if you want to use the whole thing), or use a function to limit the output accordingly. I have an old function for just this purpose.

 

e.g.

//Return a string up to a certain number of characters
//but will break on a space
function truncateString($string, $length, $ellipse='…')
{
    //Return truncated string w/ ellipse (if >$length)
    return substr($string, 0, stripos($string, ' ', $length)) . $ellipse;
}
 
$string = "This is a string with a lot of words that may need to be limited in some places in the output";
 
echo "<b>Output the full string:</b><br>\n";
echo $string . "<br><br>\n";
echo "<b>Output the limited string:</b><br>";
echo truncateString($string, 25);

Output:

 

 

Output the full string:

This is a string with a lot of words that may need to be limited in some places in the output

Output the limited string:

This is a string with a lot…

Edited by Psycho
  • Like 1
Link to comment
Share on other sites

hello dear Psycho, many many thanks for the quick reply.

 

great to hear form you!

 

fantastic to read your ideas - that sound great.

 

 

i will add your lines accordingly ... .to the part that fetches the RSS


<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');

$rss = fetch_feed('http://examplesite.com/rss');
if (!is_wp_error( $rss ) ) :

    $maxitems = $rss->get_item_quantity(5);

    $rss_items = $rss->get_items(0, $maxitems);
endif;
?>

<ul>
    <?php if ($maxitems == 0) echo '<li>No items.</li>';
    else


and then your lines come....:

 


.

//Return a string up to a certain number of characters
//but will break on a space
function truncateString($string, $length, $ellipse='…')
{
    //Return truncated string w/ ellipse (if >$length)
    return substr($string, 0, stripos($string, ' ', $length)) . $ellipse;
}
 
$string = "This is a string with a lot of words that may need to be limited in some places in the output";
 
echo "<b>Output the full string:</b><br>\n";
echo $string . "<br><br>\n";
echo "<b>Output the limited string:</b><br>";
echo truncateString($string, 25);

Output:

 

 

 

many many thanks for the hints.

 

regards

maze

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.