• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

PHP: Truncate from Left

Using PHP, how can I truncate text on the left side?
<?php

$str = 'This is a test and only a very long test to see if this works'

echo truncate_left($str,10);
// should echo 'this works';


function truncate_left($str, l) {
return ($str);
}

?>

Open in new window

0
hankknight
Asked:
hankknight
1 Solution
 
sshah254Commented:
substr($str, 0, 10)

will return 10 characters, starting from 0.

Ss
0
 
hankknightAuthor Commented:
Yea, but I want to truncate in the other direction.

This is a test and only a very long test to see if this works
Should become
this works

Test the code I posted and you will see what I mean.
0
 
Lukasz ChmielewskiCommented:
Try this:

<?php

$str = 'This is a test and only a very long test to see if this works';

print truncate_left($str,10);
// should echo 'this works';


function truncate_left($string,$count) {
 $string = substr($string, -$count, $count);
 return $string;
}

?>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now