watsmyname Posted September 14, 2009 Share Posted September 14, 2009 hello here is my truncate function that truncates if the inputted string is longer than specified characters. If its longer than specified characters, then string is returned with specified length with "..." appended in the last. This function well works on english characters. I want to use it in my language and it wont work properly because one character in our language means 5 characters in english...for example. "ा" the 7 characters in quote means a single character in my language. So if i want to show 70 characters in English, its fine, but in our language 70 characters means only 10 characters. So is there any work around for this. Here is the function <?php function truncate($str, $len, $el = '...') { if (strlen($str) > $len) { $xl = strlen($el); if ($len < $xl) { return substr($str, 0, $len); } $str = substr($str, 0, $len-$xl); $spc = strrpos($str, ' '); if ($spc > 0) { $str = substr($str, 0, $spc); } return $str.$el; } return $str; } /* where, $str=input string, $len=length to show, $el=replacing character if in case input string is bigger than specified length. */ ?> Help much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/174170-solved-help-on-truncatecustom-function/ Share on other sites More sharing options...
watsmyname Posted September 15, 2009 Author Share Posted September 15, 2009 anybody please, i just want to be treated "।" kind of characters as a single character to use truncate function i specified above.?? thanks Quote Link to comment https://forums.phpfreaks.com/topic/174170-solved-help-on-truncatecustom-function/#findComment-918734 Share on other sites More sharing options...
Psycho Posted September 15, 2009 Share Posted September 15, 2009 First you say 5 characters, then you say 7. From your examples I assume you mean seven. Are ALL of the characters in your language going to be 7 characters? If so, just multiple the length by 7. Your function is quite inefficient to begin with. Here is a function I use to do the same thing: function subStringWords($string, $length, $ellipse='...') { if (strlen($string) <= $length) { return $string; } return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse; } A simple modification will have it treat 7 characters as a single character, like so function subStringWords($string, $length, $ellipse='...') { $length = $length * 7; if (strlen($string) <= $length) { return $string; } return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse; } Now, that won't work if all elements are not represented by 7 characters. Quote Link to comment https://forums.phpfreaks.com/topic/174170-solved-help-on-truncatecustom-function/#findComment-918741 Share on other sites More sharing options...
watsmyname Posted September 15, 2009 Author Share Posted September 15, 2009 First you say 5 characters, then you say 7. From your examples I assume you mean seven. Are ALL of the characters in your language going to be 7 characters? If so, just multiple the length by 7. Your function is quite inefficient to begin with. Here is a function I use to do the same thing: function subStringWords($string, $length, $ellipse='...') { if (strlen($string) <= $length) { return $string; } return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse; } A simple modification will have it treat 7 characters as a single character, like so function subStringWords($string, $length, $ellipse='...') { $length = $length * 7; if (strlen($string) <= $length) { return $string; } return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse; } Now, that won't work if all elements are not represented by 7 characters. Thanks a lot that worked fine. How can i determine whether the input string is in my language or in english language, because i want to use same function for both English and Nepali as one row of record might be in English and another might be in my language?? Quote Link to comment https://forums.phpfreaks.com/topic/174170-solved-help-on-truncatecustom-function/#findComment-918745 Share on other sites More sharing options...
Daniel0 Posted September 15, 2009 Share Posted September 15, 2009 Use the same function for both, don't run your string through functions like htmlentities() before you pass it to your truncate string, and use the multibyte string functions instead. Quote Link to comment https://forums.phpfreaks.com/topic/174170-solved-help-on-truncatecustom-function/#findComment-918749 Share on other sites More sharing options...
watsmyname Posted September 15, 2009 Author Share Posted September 15, 2009 Use the same function for both, don't run your string through functions like htmlentities() before you pass it to your truncate string, and use the multibyte string functions instead. No that wont work, length is multiplied by 7 because 7 characters equivalent my language's one character. It wont work for english. I did something, i checked if '&' position is 0 and if ";" position is 6, then surely the string contains my language... <?php function truncate($string, $length, $ellipse='...') { $posamp=strpos($string,"&"); $possemi=strpos($string,";"); if($posamp==0 && $possemi==6) { $length = $length * 7; } if (strlen($string) <= $length) { return $string; } return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse; } ?> thanks mjdamato and daniel Quote Link to comment https://forums.phpfreaks.com/topic/174170-solved-help-on-truncatecustom-function/#findComment-918754 Share on other sites More sharing options...
Daniel0 Posted September 15, 2009 Share Posted September 15, 2009 That's what I'm telling you, don't run your string through functions like htmlentities() before you try to do things like truncation. One character is one character. It's only when you run it through htmlentities() that you'll get things like । Quote Link to comment https://forums.phpfreaks.com/topic/174170-solved-help-on-truncatecustom-function/#findComment-918764 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.