Jump to content

[SOLVED] Trim non english strings


rvdb86

Recommended Posts

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

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

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

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.