Split a text into single words

后端 未结 6 736
野趣味
野趣味 2020-11-30 07:56

I would like to split a text into single words using PHP. Do you have any idea how to achieve this?

My approach:

function tokenizer($text) {
    $tex         


        
6条回答
  •  庸人自扰
    2020-11-30 08:35

    Do:

    str_word_count($text, 1);
    

    Or if you need unicode support:

    function str_word_count_Helper($string, $format = 0, $search = null)
    {
        $result = array();
        $matches = array();
    
        if (preg_match_all('~[\p{L}\p{Mn}\p{Pd}\'\x{2019}' . preg_quote($search, '~') . ']+~u', $string, $matches) > 0)
        {
            $result = $matches[0];
        }
    
        if ($format == 0)
        {
            return count($result);
        }
    
        return $result;
    }
    

提交回复
热议问题