20
4

5 に答える 5

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);
于 2013-03-25T05:32:16.917 に答える
3

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

于 2013-03-29T14:08:23.167 に答える
1

From looking at the expression itself, there are two things that could be improved:

  1. 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 yields NULL from the preg_replace() operation.

  2. 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) "ﺫﺫ"

3v4l results

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));

3v4l results

于 2013-04-03T06:56:06.947 に答える
0

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

于 2013-04-03T02:42:02.973 に答える
0

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
    دد
于 2016-03-28T11:08:42.380 に答える