php 字符串转数组 提取中文 提取英文 字符串类型

发布时间:2020-09-22 02:00:57 作者:付离海
来源:网络 阅读:3164

获取字符串类型

/**
 * 判断中英文
 */
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 >
* #@ == </span>
*/
$message = str_replace('@#', '<span >', $message);
$message = str_replace('#@', '</span>', $message);
return $message;
}


好了,到此结束,如有问题,给我留言!

推荐阅读:
  1. php中使用频率较高的正则表达式有哪些?
  2. 如何解决php截取中文乱码的问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 字符串转数组 提取中文

上一篇:手把手教你如何使用nodejs编写cli命令行

下一篇:TCPIP路由技术卷一读书笔记之IPv6

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》