开发者

how do I trim a word?

I want trim word by almost half for example

Washington

after trim it should be washi

Chicago

after trim it should be chic

Tokyo

after trim it sho开发者_StackOverflowuld be tok


  1. Use substr() function to cut off unwanted part of the string.
  2. Use strlen() function to calculate the length of the string.
  3. Use ceil() function to round a fraction up if the length is a odd number.

    $word = 'Tokyo';
    
    echo substr($word, 0, ceil(strlen($word) / 2));
    

If you're using Unicode then use multibyte extension functions as PHP doesn't support Unicode natively.


$source = "Washington";

$word = substr($source, 0, ceil(strlen($source) / 2));

Try that..


How do you want to trim them?

E.g. you could use substr(), to get the substring you like to have.

$ret = substr('Washington', 0, 5); // $res = Washi


function halfTrim($s) {
  return substr($s, 0, ceil(strlen($s)/2));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜