rvdb86 Posted July 30, 2009 Share Posted July 30, 2009 Hi, I found this really useful code that trims a string without cutting a word in the middle: function neat_trim($str, $n, $delim='') { $len = strlen($str); if ($len > $n) { preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); return rtrim($matches[1]) . $delim; } else { return $str; } } the problem is I want to use this code on a Hebrew string and in the fututre some other languages and non-english strings don't seem to work with this function. I would really appreciate any suggestions! TIA! Link to comment https://forums.phpfreaks.com/topic/168130-solved-trim-non-english-strings/ Share on other sites More sharing options...
phpSensei Posted July 30, 2009 Share Posted July 30, 2009 Hi, I found this really useful code that trims a string without cutting a word in the middle: function neat_trim($str, $n, $delim='') { $len = strlen($str); if ($len > $n) { preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); return rtrim($matches[1]) . $delim; } else { return $str; } } the problem is I want to use this code on a Hebrew string and in the fututre some other languages and non-english strings don't seem to work with this function. I would really appreciate any suggestions! TIA! use ltrim http://us3.php.net/manual/en/function.ltrim.php Link to comment https://forums.phpfreaks.com/topic/168130-solved-trim-non-english-strings/#findComment-886709 Share on other sites More sharing options...
rvdb86 Posted July 30, 2009 Author Share Posted July 30, 2009 use ltrim Thanks for the reply but I do not understand how ltrim will allow me to trim strings without triming a word like the function allowed me to do? Link to comment https://forums.phpfreaks.com/topic/168130-solved-trim-non-english-strings/#findComment-886721 Share on other sites More sharing options...
phpSensei Posted July 30, 2009 Share Posted July 30, 2009 use ltrim Thanks for the reply but I do not understand how ltrim will allow me to trim strings without triming a word like the function allowed me to do? I dont understand how a trim "trims" inbetween wordds. I thought you ment trimming other languages like from right to left. trim — Strip whitespace (or other characters) from the beginning and end of a string unless you have these * " " (ASCII 32 (0x20)), an ordinary space. * "\t" (ASCII 9 (0x09)), a tab. * "\n" (ASCII 10 (0x0A)), a new line (line feed). * "\r" (ASCII 13 (0x0D)), a carriage return. * "\0" (ASCII 0 (0x00)), the NUL-byte. * "\x0B" (ASCII 11 (0x0B)), a vertical tab. otherwise there are far better solutions than that script provided Link to comment https://forums.phpfreaks.com/topic/168130-solved-trim-non-english-strings/#findComment-886723 Share on other sites More sharing options...
rvdb86 Posted July 30, 2009 Author Share Posted July 30, 2009 otherwise there are far better solutions than that script provided maybe you can suggest some of these solutions. I apologise for my miss use of the word trim, I meant that i was trying to make the string shorter. Link to comment https://forums.phpfreaks.com/topic/168130-solved-trim-non-english-strings/#findComment-886728 Share on other sites More sharing options...
rvdb86 Posted July 30, 2009 Author Share Posted July 30, 2009 I have found a solution for my problem, if anyone else is interested it can be found here: http://www.bitrepository.com/shorten-a-string.html Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/168130-solved-trim-non-english-strings/#findComment-886736 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.