有用的函数-字符串处理 function make_semiangle($str) { $arr = array('0' = '0', '1' = '1', '2' = '2', '3' = '3', '4' = '4', '5' = '5', '6' = '6', '7' = '7', '8' = '8', '9' = '9', 'A' = 'A', 'B' = 'B', 'C' =
有用的函数-字符串处理
- function make_semiangle($str)
- {
- $arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
- '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
- 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
- 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
- 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
- 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
- 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
- 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
- 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
- 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
- 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
- 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
- 'y' => 'y', 'z' => 'z',
- '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
- '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
- '‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '<',
- '》' => '>',
- '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
- ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
- ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
- '”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
- ' ' => ' ','$'=>'$','@'=>'@','#'=>'#','^'=>'^','&'=>'&','*'=>'*',
- '"'=>'"');
- return strtr($str, $arr);
- }
复制代码
2.字符串转译
1.字符串处理
- function Char_rev($msg,$direct=0){
- $or_str =array('&',' ','"',''','<','>'," ",""," ","<br>","<br />");
- $rep_str = array('&',' ','"',"'","<",">","\t","\r"," ","\n","\n");
- if(0==$direct)
- {$msg=str_replace($or_str,$rep_str,$msg);}
- else
- {$msg = str_replace($rep_str,$or_str,$msg);}
- return $msg;
- }
复制代码
3.计算字符串长度(汉字按两字符算)
- function str_len($str)
- {
- $length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));
- if ($length)
- {
- return strlen($str) - $length + intval($length / 3) * 2;
- }
- else
- {
- return strlen($str);
- }
- }
复制代码
4.phpcms加解密函数
- function phpcms_encode($txt, $key)
- {
- srand((double)microtime() * 1000000);
- $encrypt_key = md5(rand(0, 32000));
- $ctr = 0;
- $tmp = '';
- for($i = 0;$i < strlen($txt); $i++)
- {
- $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
- $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
- }
- return base64_encode(phpcms_key($tmp, $key));
- }
- function phpcms_decode($txt, $key)
- {
- $txt = phpcms_key(base64_decode($txt), $key);
- $tmp = '';
- for ($i = 0;$i < strlen($txt); $i++)
- {
- $md5 = $txt[$i];
- $tmp .= $txt[++$i] ^ $md5;
- }
- return $tmp;
- }
- function phpcms_key($txt, $encrypt_key)
- {
- $encrypt_key = md5($encrypt_key);
- $ctr = 0;
- $tmp = '';
- for($i = 0; $i < strlen($txt); $i++)
- {
- $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
- $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
- }
- return $tmp;
- }
复制代码
5.正则加亮关键字
- function highlight_words($str, $words, $color = '#FFFF00') {
- if(is_array($words)) {
- foreach($words as $k => $word) {
- $pattern[$k] = "/\b($word)\b/is";
- $replace[$k] = '<font style="background-color:'.$color.';">\\1</font>';
- }
- }
- else {
- $pattern = "/\b($words)\b/is";
- $replace = '<font style="background-color:'.$color.';">\\1</font>';
- }
- return preg_replace($pattern,$replace,$str);
- }
复制代码
6.view中selected 和checked的懒人处理方法
- /*
- <select><option value='a' [code]=select($a,'a')
复制代码
></option></select>
*/
function select($a,$b){
if($a==$b){
return "selected";
}else{
return "";
}
}
function radio($a,$b){
if($a==$b){
return "checked";
}else{
return "";
}
}
[/code]