CopyCode The Code is as follows:
/**
* Mona1 Word Segmentation Algorithm
* Utf8: encode the next character. If the first ASCII code is not greater than 192, it only occupies 1 byte.
* If the first ASCII code is greater than 192 or less than 224, it occupies 2 bytes; otherwise, it occupies 3 bytes.
* For one-dollar word splitting, you must add ft_min_word_len = 1 to the MySQL my. ini file.
* You can use the MySQL query statement show variables like '% FT %' to view MySQL full-text search settings.
*
* @ Access global
* @ Param string $ Str
* @ Param Boolean $ whether to remove duplicate values
* @ Param Boolean $ whether merge combines additional value
* @ Return Array
*/
Function seg_word ($ STR, $ unique = false, $ merge = true)
{
$ STR = trim (strip_tags ($ Str ));
$ Strlen = strlen ($ Str );
If ($ strlen = 0) return array ();
$ SPC = '';
// Add the characters to be filtered as needed
$ Search = array (',','/','\\','.',';',':','\'','! ','~ ',' "', ''',' ^ ','(',')','? ','-', "\ T", "\ n",' \ '', '<', '>'," \ r ", "\ r \ n", '\ $', '&', '%', '#', '@', '+', '= ','{', '}', '[', ']', ')', '(', '. ','. ',',','! ','; ',' [','] ','-', ','-','… ','【',']',':');
$ Numpairs = array ('1' => '1', '2' => '2', '3' => '3 ', '4' => '4', '5' => '5', '6' => '6', '7' => '7 ', '8' => '8', '9' => '9', '0' => '0 ');
$ STR = alab_num ($ Str );
$ STR = str_replace ($ search, '', $ Str );
$ Ord = $ I = $ k = 0;
$ Prechar = 0; // 0-blank 1-English and symbol 2-Chinese
$ Result = array ();
$ Annex = array ();
While ($ ord = ord ($ STR [$ I])
{
// 1 byte
If ($ ord <= 0xc0)
{
// Remove null strings
If ($ ord <33 ){
$ Prechar = 0;
$ I ++;
$ K ++;
Continue;
}
// Add Chinese uppercase numbers for conversion
If (isset ($ numpairs [$ STR [$ I]) {
$ Annex [] = $ numpairs [$ STR [$ I];
}
// If the front is Chinese
If ($ prechar = 2 ){
$ Result [+ $ K] = $ STR [$ I];
}
Else {
$ Result [$ K]. = $ STR [$ I];
}
$ Prechar = 1;
$ I ++;
}
Else // 2-3 bytes (Chinese)
{
If ($ ord <0xe0)
$ Step = 2;
Else
$ Step = 3;
$ C = substr ($ STR, $ I, $ step );
If (false! ==$ Key = array_search ($ C, $ numpairs )){
$ Annex [] = $ key;
}
If ($ prechar! = 0 ){
$ Result [++ $ K] = $ C;
}
Else {
$ Result [$ K]. = $ C;
}
$ Prechar = 2;
$ I + = $ step;
}
}
$ Result = $ merge? Array_merge ($ result, $ annex): $ result;
Return $ unique? Array_unique ($ result): $ result;
}