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;// 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", '@#\1#@',$message);
}
/**
*
* @# == <span style="color:#ff0000">
* #@ == </span>
*/
$message = str_replace('@#', '<span style="color:'.$color.'">', $message);
$message = str_replace('#@', '</span>', $message);
return $message;
}
はい、これで終わります.問題があれば、メッセージをください.