Split a text into single words

后端 未结 6 742
野趣味
野趣味 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()

提交回复
热议问题