Jump to content

[SOLVED] "rtrim" specific length from sentence


salman_ahad@yahoo.com

Recommended Posts

Do you want to preform rtrim on characters on the right of the sentence or do you want to completely remove them?

 

It would be better to replace those $a(int) number of characters at the end with "...Read More"

 

or

 

worst case remove them.

Then you should go with substr() as Thorpe said.

 

ex

echo substr($sentence, 0, $a) . ' Read more...';

 

The problem with that is it will cut in the middle of words. Alternatively to get around this you can use this:

 

echo current(explode("\n", wordwrap($sentence, $a))) . ' Read more...';

 

wordwrap()

I can't speak for your specific case, but it seems more logical that you limit the amount of characters starting from the left. If you have strings that vary in length then you'll have different results. Wouldn't you want to have the strings with the 'Read more..' be the same length all the time? At least that's normally how it is.

I can't speak for your specific case, but it seems more logical that you limit the amount of characters starting from the left....

 

I know what you are saying, so I reversed my logic

 

//say 100 is total length
$a= //some int length
$b= 100 - $a

$result = current(explode("\n", wordwrap($sentence, $b))) . ' Read more...';

//Now I have $sentence[] as array...how do I do now...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.