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
- Use
substr()
function to cut off unwanted part of the string. - Use
strlen()
function to calculate the length of the string. 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));
}
精彩评论