[原]阿拉伯数字金额转中文大写函数(2 位领导批示)

最近正好用到.就自己写了个.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function chinese_money($money)
{
    $str = array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
    $unit = array('零','分','角','元','拾','佰','仟','万','拾','佰','仟','亿','拾','佰','仟');
    $money = str_replace(",","",$money);
    $money = number_format($money,2);
    $money = str_replace(".","",$money);
    $money = str_replace(",","",$money);
    $f = $money;
 
    $money = abs($money);
    if($f != $money) $m = '负';
    $len = strlen($money);
    for ($i=1; $i<=$len; $i++)
    {
        $num = substr($money,$i-1,1);
        $j = strlen($money)+1-$i;
        $m .= $num . $unit[$j];
    }
    foreach($str as $key=>$val)
    {
        $m = str_replace($key,$val,$m);
    }
    return $m;
}
 
$money = 9123123.12;
echo $money . "\n";
echo chinese_money($money);

[收集]一个全角半角转换函数(0 位领导批示)

strtr 的效率据说比 str_replace 高四倍.这个全角半角转换函数的核心便是 strtr 函数

函数原型

string strtr (string str, string from, string to) 或 string strtr (string str, array replace_pairs)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
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);   
}
?>

(备忘)自己写的UBB2HTML与HTML2UBB函数(2 位领导批示)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
function ubb2html($str)
{
    $str = htmlspecialchars(stripslashes($str));
    // 字体颜色
    $str = preg_replace("#\[color\=([^\]]*)\]([^\[]*)#i","<span style='color:$1'>$2",$str);
    $str = preg_replace("#\[\/color\]#i","</span>",$str);
    // 字体大小
    $str = preg_replace("#\[size\=(\d)\]([^\[]*)#i","<font size='$1'>$2",$str);
    $str = preg_replace("#\[\/size\]#i","</font>",$str);
    // 超链接
    $str = preg_replace("#\[url\=([^\]]*)\]([^\[]*)#i","<a href='$1' target='_blank'>$2",$str);
    $str = preg_replace("#\[\/url\]#i","</a>",$str);
    // 图片
    $str = preg_replace("#\[img\]([^\[]*)\[\/img\]#i","<img src='$1' />",$str);
    // 其它
    $str = preg_replace("#\[([\/]?)b\]#i","<$1strong>",$str);
    $str = preg_replace("#\[([\/]?)i\]#i","<$1i>",$str);
    $str = preg_replace("#\[([\/]?)em\]#i","<$1em>",$str);
    $str = preg_replace("#\[([\/]?)u\]#i","<$1u>",$str);
    return nl2br($str);
}

全文阅读 »

JS中文字符串转换unicode编码函数(0 位领导批示)

AJAX传递中文字符串时必须把中文字符串编码成unicode,一般会用到JS的自带函数escape().不过找到了更好的函数来确决中文字符转换成unicode编码的函数

  1. function uniencode(text)
  2. {
  3.     text = escape(text.toString()).replace(/\+/g, "%2B");
  4.     var matches = text.match(/(%([0-9A-F]{2}))/gi);
  5.     if (matches)
  6.     {
  7.         for (var matchid = 0; matchid < matches.length; matchid++)
  8.         {
  9.             var code = matches[matchid].substring(1,3);
  10.             if (parseInt(code, 16) >= 128)
  11.             {
  12.                 text = text.replace(matches[matchid], '%u00' + code);
  13.             }
  14.         }
  15.     }
  16.     text = text.replace('%25', '%u0025');
  17.  
  18.     return text;
  19. }

当然服务器端要对编码过的字符串进行第二次转码.把字符串转换成UTF-8编码.

全文阅读 »

随机显示的10篇日志

评论最多的10篇日志

浏览最多的10篇日志