PHP string-to-array extraction Chinese extract English string type

Source: Internet
Author: User

Get String type

/** *  Judgment in English  */public function checkstr ($str) {$output  =  ";  ///\<[\s \s]*\>/i$a = preg_match ('/[' &NBSP;.&NBSP;CHR (0xa1)  .  '-' &NBSP;.&NBSP;CHR (0xff)  .   ']/i ',  $str); $b  = preg_match ('/[0-9]/i ',  $str); $c  = preg_match ('/[a-za-z]/ I ',  $str), if ($a  &&  $b  &&  $c) {$output  = 1; //' A mixed string of Chinese numerals in English ';} ElseIf ($a  &&  $b  && ! $c) {$output A mixed string of  = 2; //' kanji numbers ';} ElseIf ($a  && ! $b  &&  $c) {$output A mixed string of  = 3; //' kanji English;} ElseIf (! $a  &&  $b  &&  $c) {$output  = 4; //' digital English mixed string ';} ElseIf ($a  && ! $b  && ! $c) {$output  = 5; //' pure kanji ';} ElseIf (! $a  &&  $b  && ! $c) {$output  = 6; //' pure numbers ';} ElseIf (! $a  && ! $b &NBSP;&AMP;&Amp;  $c) {$output  = 7; //' plain English ';} return  $output;}



Extract Chinese

/** *  *  only extract Chinese name  *  @param   $srchKeyword  */public function  GETCN ($srchKeyword) {$preg _repone = array ("|[ 0-9a-za-z/-]+| ",  '/\ (|\)/',  '/\" |\ "/',  '/\+|\-|\*|\//'  ,  '/\{|\}/',  '/\<|\ >/',  '/\ "|\"/',  '/\[|\]/',  '/\ (|\)/'); $preg _reptwo = array ('   ', php_eol, ' + ',      '-',  ' \ ',  '/',  ' \ \ ',  ' [',  '] ',  ' {',  '} ',  ' è ',   ' beta ',  ' ± ',  ' ^ ',  ',  ' ~ ',  '-',  ' + ',  '. ',  '. ',  ' ',  ' # ',  ' @ ',  '! ',  ' & ',  ' * ',  ' < ',  ' > ',  '% ',  ' α ',  ' gamma ',  ' ε ',  ',  ' <i> ',  ' </i> '  ,  ' [',  '] ',  '-',   ' δ ',  ' + ',  ' "',  '" ',  ' # ',  '; ',  ' \\r ',  ' \′ ',  ' ',  ', ',  ' _ '), $str  = preg_replace ($preg _repone, array (", "),  $srchKeyword); $str   = str_replace ($preg _reptwo, array (", "),  $str); $str  = mb_substr ($str, 0,255, ' Utf-8 '); Return trim ($STR);}



Extract English

/** *  extract English  *  @param   $srchKeyword  */public function geten ($srchKeyword) {$preg _repone = array ('/[-][a-za-z][0-9]/i ',  '/[0-9]/i ',  '/[a-za-z][0-9][-]/i ',  '/[^\ [0-9]da-za-z]/i ',  '/\ (|\)/',  '/\ "|\"/',  '/\+|\-|\*|\//'  ,  '/\{|\}/',  '/\<|\ >/',  '/\ "|\"/',  '/\[|\]/',  '/\ (|\)/'); $preg _reptwo = array ('   ',php_eol,      '-',  ' \ ',  '/',  ' \ ',  ' [',  '] ',  ' {',  '} ',  ' è ',  ' Beta ',  ' ± ',  ' ^ ',  ' ',  ' ~ ',  '-',  ' + ',  '. ',  '. ',  ' ',  ' # ',  ' @ ',  '! ',  ' & ',  ' * ',  ' < ',  ' > ',  '% ',  ' α ',  ' gamma ',  ' ε ',  ',  ' <i> ',  ' </i> '  ,  ' [',  '] ',  '-',   ' δ ',  ' + ',  ' "',  '" ',  ' # ',  '; ',  ' \\r ',  ' \′ ',  ' ',  ' (',  ') ) ',  ' (',  ') ',  ', ',  ' _ '); $Str = preg_replace ($preg _repone, array (", "),  $srchKeyword); $str  = str_ Replace ($preg _reptwo, array (',  '),  $str); $str  = mb_substr ($str,  0, 255,   ' Utf-8 '); Return strtolower ($STR);}



Array of string turns

/*** string to array * @param $key _words Incoming String * @param $type String type */function Get_key_arr ($key _words, $type = ' en ') {$ex _ = 1; $str _len = strlen ($key _words); $return _arr = ", if ($type = = ' cn ') {$ex _ = 3; $str _num = Ceil ($str _len/$ex _); for ($i = 1; $i <= $st R_num; $i + +) {$start = ($i-1) * $ex _; $return _arr [] = substr ($key _words, $start, $ex _);}} Else{for ($i = 0; $i < $str _len; $i + +) {$return _arr [] = $key _words[$i];}} $return _arr = Array_unique ($return _arr); return $return _arr;}



Highlight Display

/** * Highlight function * @param $message need to highlight the character string * @param $str need to highlight the part of the string * @param $color Highlight color */function bat_highlight ($ Message, $str, $color = ' #ff0000 ') {$checkstr = $this->checkstr ($STR); if ($checkstr = = 7 | | $checkstr = = 4) {//full-Text Search $words_ Info_en = $this->geten ($str), $return _arr = $this->get_key_arr ($words _info_en);} ElseIf ($checkstr = = 1 | | $checkstr = = 2 | | $checkstr = = 3 | | $checkstr = = 5) {//full-Text Search $WORDS_INFO_CN = $this-&GT;GETCN ($STR); $ Return_arr = $this->get_key_arr ($words _info_cn, ' cn ');} foreach ($return _arr as $value) {$message = Preg_replace ("/($value)/i", ' <span style= "color: '. $color. '" >\1</span> ', $message);} Retrun $message;}


Well, to this end, if there is a problem, give me a message!

This article is from the "Like Here" blog, please be sure to keep this source http://liuzan.blog.51cto.com/6103676/1561279

PHP string-to-array extraction Chinese extract English string type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.