24

I'm trying to truncate some text in PHP and have stumbled across this method (http://theodin.co.uk/blog/development/truncate-text-in-php-the-easy-way.html), which judging by the comments seems like a great easy-to-implement solution. The problem is I don't know how to implement it :S.

Would someone mind pointing me in the direction of what to do to implement this? Any help whatsoever would be appreciated.

Thanks in advance.

realph
  • 4,481
  • 13
  • 49
  • 104
  • 1
    What don't you understand about `substr()`? Where are you getting confused? It is one function that returns part of the source string, based on the parameters you give it. – Brad Feb 09 '12 at 22:27
  • 1
    Yes, just use `substr()` the first parameter is your text, the second is the offset - if set to 0 it will truncate from the beginning if set to 1,2,3... it will truncated after that many characters, the third argument is the length that should be truncated. For example `substr("hello world", 3, 4)` will return `lo w` - 4 characters after 3. – Tony Bogdanov Feb 09 '12 at 22:30
  • possible duplicate of [Truncate a multibyte String to n chars](http://stackoverflow.com/questions/2154220/truncate-a-multibyte-string-to-n-chars) – Gordon Feb 09 '12 at 22:51
  • 1
    Closed as not a real question? The poster wants to know how to truncate text in PHP. Are a bunch of monkeys dictating what a question is, nowadays? – BxtchCraft Sep 14 '14 at 23:14

3 Answers3

67

The obvious thing to do is read the documentation.

But to help: substr($str, $start, $end);

$str is your text

$start is the character index to begin at. In your case, it is likely 0 which means the very beginning.

$end is where to truncate at. Suppose you wanted to end at 15 characters, for example. You would write it like this:

<?php

$text = "long text that should be truncated";
echo substr($text, 0, 15);

?>

and you would get this:

long text that 

makes sense?

EDIT

The link you gave is a function to find the last white space after chopping text to a desired length so you don't cut off in the middle of a word. However, it is missing one important thing - the desired length to be passed to the function instead of always assuming you want it to be 25 characters. So here's the updated version:

function truncate($text, $chars = 25) {
    if (strlen($text) <= $chars) {
        return $text;
    }
    $text = $text." ";
    $text = substr($text,0,$chars);
    $text = substr($text,0,strrpos($text,' '));
    $text = $text."...";
    return $text;
}

So in your case you would paste this function into the functions.php file and call it like this in your page:

$post = the_post();
echo truncate($post, 100);

This will chop your post down to the last occurrence of a white space before or equal to 100 characters. Obviously you can pass any number instead of 100. Whatever you need.

Community
  • 1
  • 1
Kai Qing
  • 18,793
  • 5
  • 39
  • 57
  • definitely. Almost positively a really bad one. – Kai Qing Feb 09 '12 at 22:36
  • Thanks for the help, I'm trying to make sense of this. Apologies in advance if I come off stupid. So would I put that string from the website I linked to into something like a functions.php file (I'm using WordPress)? What I'm saying is where do I declare that function? – realph Feb 09 '12 at 22:42
  • substr is a native php function so you can call it on the fly anywhere. No need to write a function that returns the same thing unless you're expanding it for some reason. Sounds like you are not. wordpress, for example, lets you do things like this: $post = the_post(); where you define the content of a post as a variable. You could substr that and see how it reacts. echo substr($post, 0, 100); and see if it returns the first 100 characters of your post. If this is not what you mean, you may want to edit your question to be more specific about your exact situation. – Kai Qing Feb 09 '12 at 22:46
  • I'm sorry, I've totally missed the point. Thanks for this! Seems to work a treat. Apart from obviously being shorter, how does this method differ from the link I posted? – realph Feb 09 '12 at 22:48
  • Ah, gotcha! That would be great! I'm trying to work out now how to end it with "...". I'll try and work it out myself, and post my results back here. Thanks again. – realph Feb 09 '12 at 22:54
  • I have updated my answer to explain. – Kai Qing Feb 09 '12 at 22:57
  • Huge thanks for the update. Would that be a more streamlined way to do it? Since 100 is passed in the PHP, what use is the number 25 in the functions? – realph Feb 09 '12 at 23:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7511/discussion-between-realph-and-kai-qing) – realph Feb 09 '12 at 23:08
  • 10
    php's mb_strimwidth function exactly does same thing with single built-in function: `mb_strimwidth($string, 0, 15, "...")` – tolginho May 29 '16 at 10:15
  • I would add if (strlen($text) > $chars) { ... } in the function to make sure that if the length of the string is less than the desired characters, you don't add the ellipse. – Christopher Smit Nov 03 '17 at 06:53
  • @ChristopherSmit - yeah there's a lot of ways this could be improved upon. It's basically just a starter function. Think of what happens if there's any html in the string. do you strip tags, or do you write an intelligent parser to split logically and close the unclosed tags? – Kai Qing Nov 03 '17 at 16:28
7
$mystring = "this is the text I would like to truncate";

// Pass your variable to the function
$mystring = truncate($mystring);

// Truncated tring printed out;
echo $mystring;

//truncate text function
public function truncate($text) {

    //specify number fo characters to shorten by
    $chars = 25;

    $text = $text." ";
    $text = substr($text,0,$chars);
    $text = substr($text,0,strrpos($text,' '));
    $text = $text."...";
    return $text;
}
thenetimp
  • 9,487
  • 5
  • 29
  • 42
5
$text="abc1234567890";

// truncate to 4 chars

echo substr(str_pad($text,4),0,4);

This avoids the problem of truncating a 4 char string to 10 chars .. (i.e. source is smaller than the required)

FreudianSlip
  • 2,870
  • 25
  • 24