5 に答える
You can use alternative mb_ereg_replace() function:
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
echo mb_ereg_replace('#(?:^[^\pL]*)|(?:[^\pL]*$)#u','',$string);
maybe this will help :
these properties are usualy only available if PCRE is compiled with "--enable-unicode-properties"
http://docs.php.net/manual/en/regexp.reference.unicode.php#96479
From looking at the expression itself, there are two things that could be improved:
The
*
multipliers aren't very useful; why would you want to replace a potentially empty match with an empty string? In fact, running this on my system yieldsNULL
from thepreg_replace()
operation.The memory groups can be merged together.
This is the code after applying both improvements:
$string = 'ﺫﺫ';
var_dump(preg_replace('#(?:^[^\pL]+|[^\pL]+$)#u', '', $string));
// string(4) "ﺫﺫ"
If you're just looking for a multibyte trim function (supported from 4.3.0 onwards):
$string=' دد';
var_dump(preg_replace('#(?:^\s+|\s+$)#u', '', $string));
Use preg_quote
and you have to properly escape the special character before using it with your regex. For example:
<?php
$string = preg_quote("\دد");
echo preg_replace('#(?:^[^\pL]*)|(?:[^\pL]*$)#u','',$string);
See it in action: http://3v4l.org/LeBXg
More about preg_quote.
Cheers,
Ardy
Lastly, the bug was solved:
Output for 4.4.0 - 4.4.9, 5.0.5 - 5.1.6, 5.5.27 - 5.5.33, 5.6.11 - 7.0.4, hhvm-3.6.1 - 3.12.0 دد