Unfortunatly trim()
is not part of mbstring's function set (mb_*
). Otherwise you could simply enable mbstring's Function Overloading Feature.
But thanks to PHP's namespace fallback policy it is possible:
For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist.
I.e. you can override trim()
(not \trim()
). You have to use namespaces and call trim without explicitly prefixing the global namespace (i.e. no \
prefix).
namespace myns;
function trim($str, $charlist=" ") {
$pregCharacters = preg_quote($charlist);
return preg_replace("/^[$pregCharacters]+|[$pregCharacters]+$/", '', $str);
}
var_dump(trim(" a b c "));
Didn't think too much about that RegExp. It should just illustrate overriding of trim()
.
AFAIK the only thing you have to take care of is that the definition of \myns\trim()
should happen before your first trim()
call. This is a very attractive technique for mocking time()
in unit tests.
Regarding your second question, \s
would match U+3000 if you turn on the u
-switch (PCRE_UTF8):
var_dump(preg_match("/\s/u", " "));