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

LVL 16
hankknightAsked:
Who is Participating?
 
Lukasz ChmielewskiConnect With a Mentor Commented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.