Split a text into single words

后端 未结 6 717
野趣味
野趣味 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:31

    you can also use PHP strtok() function to fetch string tokens from your large string. you can use it like this:

     $result = array();
     // your original string
     $text = 'This is an example text, it contains commas and full stops. Exclamation marks, too! Question marks? All punctuation marks you know.';
     // you pass strtok() your string, and a delimiter to specify how tokens are separated. words are seperated by a space.
     $word = strtok($text,' ');
     while ( $word !== false ) {
         $result[] = $word;
         $word = strtok(' ');
     }
    

    see more on php documentation for strtok()

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-30 08:42

    You can also use the method explode : http://php.net/manual/en/function.explode.php

    $words = explode(" ", $sentence);
    
    0 讨论(0)
  • 2020-11-30 08:45

    Use the class \p{P} which matches any unicode punctuation character, combined with the \s whitespace class.

    $result = preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $text, -1, PREG_SPLIT_NO_EMPTY);
    

    This will split on a group of one or more whitespace characters, but also suck in any surrounding punctuation characters. It also matches punctuation characters at the beginning or end of the string. This discriminates cases such as "don't" and "he said 'ouch!'"

    0 讨论(0)
  • 2020-11-30 08:46

    I would first make the string to lower-case before splitting it up. That would make the i modifier and the array processing afterwards unnecessary. Additionally I would use the \W shorthand for non-word characters and add a + multiplier.

    $text = 'This is an example text, it contains commas and full stops. Exclamation marks, too! Question marks? All punctuation marks you know.';
    $result = preg_split('/\W+/', strtolower($text), -1, PREG_SPLIT_NO_EMPTY);
    

    Edit   Use the Unicode character properties instead of \W as marcog suggested. Something like [\p{P}\p{Z}] (punctuation and separator characters) would cover the characters more specific than \W.

    0 讨论(0)
  • 2020-11-30 08:47

    Tokenize - strtok.

    <?php
    $text = 'This is an example text, it contains commas and full stops. Exclamation marks, too! Question marks? All punctuation marks you know.';
    $delim = ' \n\t,.!?:;';
    
    $tok = strtok($text, $delim);
    
    while ($tok !== false) {
        echo "Word=$tok<br />";
        $tok = strtok($delim);
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题