Jump to content

Excerpt function problem


daniel.s
 Share

Recommended Posts

Hello,

I'm using a function to limit an entry to a predetermined number of characters. This is what it looks like:

<?php
    
    // Excerpts a field to $limit length
    
    function excerpt($str, $limit = 400, $endstr = '…'){
    $str = strip_tags($str);
    if(strlen($str) <= $limit) return $str;
    $out = substr($str, 0, $limit);
    $pos = strrpos($out, " ");
    if ($pos>0) {
    $out = substr($out, 0, $pos);
    }
    return $out .= $endstr;
    }

?>

It works like a charm but sometimes i want to limit two different fields within the same function:

<?php echo excerpt($result->summary. $result->body); ?>

This works 'OK' but this way i can't get a space between the summary text and the body text. It's i minor issue but it looks kinda dull. For example: "Hello, this is the summary.And this is the body text."

My PHP knowledge is low but my guess is that the problem arises with the "strip_tags" function. Is there any way around this? 

Thanks for help

/Daniel

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...