Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncating Text in PHP? [closed]

Tags:

php

truncate

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.

like image 496
realph Avatar asked Feb 09 '12 22:02

realph


People also ask

How to truncate text in php?

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.

How to truncate data in php?

The ftruncate() function in PHP is an inbuilt function which is used to truncate(shorten) an open file to the specified length. The file and the new size of the file are sent as parameters to the ftruncate() function and it returns True on success and False on Failure.

How do I restrict words in php?

A string is a sequence of characters in PHP. The length of the string can be limited in PHP using various in-built functions, wherein the string character count can be restricted. Approach 1 (Using for loop): The str_split() function can be used to convert the specified string into the array object.

How can I truncate a string to the first 20 words in php?

php $str = "this is a string that is just some text for you to test with"; print(truncateString($str, 20, false) . "\n"); print(truncateString($str, 22, false) .


3 Answers

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.

like image 200
Kai Qing Avatar answered Oct 09 '22 23:10

Kai Qing


$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;
}
like image 28
thenetimp Avatar answered Oct 09 '22 23:10

thenetimp


$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)

like image 5
FreudianSlip Avatar answered Oct 09 '22 23:10

FreudianSlip