PHP : Remove words less than 3 characters in unicode text

前端 未结 2 637

I use these regex to remove words less than 3 characters :

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

and

$         


        
2条回答
  •  悲哀的现实
    2021-01-26 08:12

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

提交回复
热议问题