Reputation: 1697
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()
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.
Upvotes: 1
Views: 641
Reputation: 145512
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.
Upvotes: 1
Reputation: 71
This should be all you need:
function remWhite($str) {
$value = preg_replace('/\s+/', '',$str);
return $value;
}
Upvotes: 0
Reputation: 5796
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.
Upvotes: 1
Reputation: 67
trim() handles the following whitespace characters, by default:
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()
Upvotes: 0
Reputation: 37543
Use the ltrim()
method.
http://www.php.net/manual/en/function.ltrim.php
Upvotes: 1