PHP:
I want to trim()
the whitespace off the beginning of a string using php, but the default settings in trim()
aren't handling the the whitespace characters. The white space is 7 spaces long. Is there a way to remove the 7 spaces using trim()
开发者_JS百科or another function?
A function that trims everything until a "normal" character (quotes as in I don't know what to call a normal character) is encountered would work.
Use the ltrim()
method.
http://www.php.net/manual/en/function.ltrim.php
the php function trim() does, following its specification, remove all whitespace characters from the beginning and end of a string, where whitespace includes space, tab, newlines and zero byte. I guess you must be using it incorrectly then? Some code would definitley help solving your problem.
To get rid of all sorts of evil whitespaces (there is more than just the ASCII variants) you can use preg_replace
:
$str = preg_replace('/^\pZ+/u', '', $str);
If that doesn't work then you'll have to look into the actual characters first. Use bin2hex()
or an hexeditor of your choosing to inspect what you actually have in your string.
trim() handles the following whitespace characters, by default:
- " " (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.
this would imply that your whitespace is not one of these. If you can figure out what kind of whitespace it is, you can supply it as an argument to trim()
This should be all you need:
function remWhite($str) {
$value = preg_replace('/\s+/', '',$str);
return $value;
}
精彩评论