php 字串轉數組 提取中文 提取英文 字串類型

來源:互聯網
上載者:User

標籤:php 字串轉數組 提取中文 提取英文 擷取字串類型

擷取字串類型

/** * 判斷中英文 */public function checkStr($str){$output = ‘‘; ///\<[\s\S]*\>/i$a = preg_match(‘/[‘ . chr(0xa1) . ‘-‘ . 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; //‘漢字數字英文的混合字串‘;}elseif($a && $b && !$c){$output = 2; //‘漢字數位混合字串‘;}elseif($a && !$b && $c){$output = 3; //‘漢字英文的混合字串‘;}elseif(!$a && $b && $c){$output = 4; //‘數字英文的混合字串‘;}elseif($a && !$b && !$c){$output = 5; //‘純漢字‘;}elseif(!$a && $b && !$c){$output = 6; //‘純數字‘;}elseif(!$a && !$b && $c){$output = 7; //‘純英文‘;}return $output;}



 提取中文

/** *  * 只提取中文名稱 * @param $srchKeyword */public function getCN($srchKeyword){$preg_repOne = array("|[0-9a-zA-Z/-]+|", ‘/\(|\)/‘, ‘/\《|\》/‘, ‘/\+|\-|\*|\//‘ , ‘/\{|\}/‘, ‘/\<|\>/‘, ‘/\【|\】/‘, ‘/\[|\]/‘, ‘/\(|\)/‘);$preg_repTwo = array(‘ ‘,PHP_EOL,‘+‘,    ‘-‘, ‘\‘‘, ‘/‘, ‘\\‘, ‘[‘, ‘]‘, ‘{‘, ‘}‘, ‘è‘, ‘β‘, ‘±‘, ‘^‘, ‘’‘, ‘~‘, ‘-‘, ‘+‘, ‘.‘, ‘。‘, ‘\‘‘, ‘#‘, ‘@‘, ‘!‘, ‘&‘, ‘*‘, ‘<‘, ‘>‘, ‘%‘, ‘α‘, ‘γ‘, ‘ε‘, ‘‘, ‘<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);}



提取英文

/** * 提取英文 * @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,    ‘-‘, ‘\‘‘, ‘/‘, ‘\\‘, ‘[‘, ‘]‘, ‘{‘, ‘}‘, ‘è‘, ‘β‘, ‘±‘, ‘^‘, ‘’‘, ‘~‘, ‘-‘, ‘+‘, ‘.‘, ‘。‘, ‘\‘‘, ‘#‘, ‘@‘, ‘!‘, ‘&‘, ‘*‘, ‘<‘, ‘>‘, ‘%‘, ‘α‘, ‘γ‘, ‘ε‘, ‘‘, ‘<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);}



字串轉數組

/*** 字串轉數組* @param $key_words 傳入的字串* @param $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 <= $str_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;}



高亮顯示

/** * 高亮顯示函數 * @param  $message 需要高亮字的符串 * @param  $str    需要高亮的部分字串 * @param  $color   高亮顯示顏色 */function bat_highlight($message,$str,$color = ‘#ff0000‘){$checkstr = $this->checkStr($str);if($checkstr== 7 || $checkstr == 4){//全文檢索索引$words_info_en = $this->getEN($str);$return_arr = $this->get_key_arr($words_info_en);}elseif($checkstr== 1 || $checkstr == 2 || $checkstr == 3 || $checkstr == 5){//全文檢索索引$words_info_cn = $this->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;}


好了,到此結束,如有問題,給我留言!

本文出自 “喜歡這裡” 部落格,請務必保留此出處http://liuzan.blog.51cto.com/6103676/1561279

php 字串轉數組 提取中文 提取英文 字串類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.