salman_ahad@yahoo.com Posted October 30, 2009 Share Posted October 30, 2009 <?php $a = //some int value $sentence = "This is the string. Need to rtrim $a number of characters from right of $sentence"; $result_string = rtim($sentence, $a); //can we do this ?? ?> Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/ Share on other sites More sharing options...
trq Posted October 30, 2009 Share Posted October 30, 2009 Take a look at substr. Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948095 Share on other sites More sharing options...
Alex Posted October 30, 2009 Share Posted October 30, 2009 Do you want to preform rtrim on characters on the right of the sentence or do you want to completely remove them? Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948096 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 30, 2009 Author Share Posted October 30, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948098 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 30, 2009 Author Share Posted October 30, 2009 I was able to remove $result = substr($sentence, 0, -$a); Any way of adding "read more" at the end ?? Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948100 Share on other sites More sharing options...
Alex Posted October 30, 2009 Share Posted October 30, 2009 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() Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948101 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 30, 2009 Author Share Posted October 30, 2009 ex echo current(explode("\n", wordwrap($sentence, $a))) . ' Read more...'; The problem with this is wordwrap's $a characters from left. But we need to remove from right. Any solution ?? Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948121 Share on other sites More sharing options...
trq Posted October 30, 2009 Share Posted October 30, 2009 Its always a good idea to look at the examples posted in the manual. Like this one for instance. Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948123 Share on other sites More sharing options...
Alex Posted October 30, 2009 Share Posted October 30, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948126 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 31, 2009 Author Share Posted October 31, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948136 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 31, 2009 Author Share Posted October 31, 2009 $topic_bit_length = strlen($dataArray1['topic'][$y]."::".$bit); $a = $topic_bit_length + 10; $b = 100 - $a; $sent_shrink = current(explode("\n", wordwrap($dataArray2['sent'][$z], $b))).' Read more'; Quote Link to comment https://forums.phpfreaks.com/topic/179692-solved-rtrim-specific-length-from-sentence/#findComment-948195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.