开发者

PHP : Remove words less than 3 characters in unicode text

开发者 https://www.devze.com 2023-03-12 18:13 出处:网络
I use these regex to remove 开发者_Python百科words less than 3 characters : $str = preg_replace(\"!\\\\b\\\\w{1,3}\\\\b!\", \"\", $str);

I use these regex to remove 开发者_Python百科words less than 3 characters :

$str = preg_replace("!\\b\\w{1,3}\\b!", "", $str);  

and

$rdu = "/\b[^\b]{1,2}\b/";
$str = preg_replace($rdu , " ", " " . $str . " "); 

but in unicode text return me :

� �� �� �������� ��� �� � �� �� �������� ��� �� 
....

is there any way with or without regex to remove words less than 3 characters in unicode text?

THXA


Use the u modifier for UTF-8 support:

/\b\w{1,2}\b/u


function RemoveLess($String,$Char=2)
{

    $StringArray=explode (" ",$String);

    foreach ($StringArray as &$Word) 
    {
        if (mb_strlen($Word,"UTF-8")>$Char)
        {
            $Str.=$Word." ";
        }
    }

    return trim($Str);

}


$text="any text here - لا اله إلا الله محمد رسول الله";

echo RemoveLess($text);
0

精彩评论

暂无评论...
验证码 换一张
取 消