PHP开发程序应该注意的42个优化准则
PHP 独特的语法混合了 C、Java、Perl 以及 PHP 自创新的语法。它可以比 CGI或者Perl更快速的执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML文档中去执行,执行效率比完全生成 HTML标记的CGI要高许多。下面介绍了PHP开发程序应该注意的42个优化准则。
PHP 独特的语法混合了 C、Java、Perl 以及 PHP 自创新的语法。它可以比 CGI或者Perl更快速的执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML文档中去执行,执行效率比完全生成 HTML标记的CGI要高许多。下面介绍了PHP开发程序应该注意的42个优化准则。
使用终端ssh登录Linux操作系统的控制台后,会出现一个提示符号(例如:#或~),在这个提示符号之后可以输入命令,Linux根据输入的命令会做回应,这一连串的动作是由一个所谓的Shell来做处理。
Shell是一个程序,最常用的就是Bash,这也是登录系统默认会使用的Shell。
jQuery有个方法$.fn.serialize,可将表单序列化成字符串;还有个方法$.fn.serializeArray,可将表单序列化成数组。那如果要将表单序列化成对象或者JSON格式数据,该如何操作呢?
/**
* 字符串截取,支持中文和其他编码
* static
* access public
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param string $suffix 截断显示字符
* return string
*/
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true, $strip_tags=false){
if($strip_tags){
$str = strip_tags($str);//去除html标记
$pattern = "/&[a-zA-Z]+;/";//去除特殊符号
$str = preg_replace($pattern,'',$str);
}
if(function_exists("mb_substr")){
$slice = mb_substr($str, $start, $length, $charset);
}elseif(function_exists("iconv_substr")){
$slice = iconv_substr($str, $start, $length, $charset);
if(false === $slice){
$slice = '';
}
}else{
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("", array_slice($match[0], $start, $length));
}
return $suffix ? $slice.'...' : $slice;
}
$str = '2016-11-23';
function setDateCn($str){
$month = str_split(date('m',strtotime($str)));
$month = implode('十', $month);
$month = str_replace('十0', '十', $month);
$month = str_replace('1十', '十', $month);
$month = str_replace('0十', '', $month);
$day = str_split(date('d',strtotime($str)));
$day = implode('十', $day);
$day = str_replace('十0', '十', $day);
$day = str_replace('1十', '十', $day);
$day = str_replace('0十', '', $day);
return str_replace(str_split('0123456789'), str_split('〇一二三四五六七八九',3), date('Y',strtotime($str)).'年'.$month.'月'.$day).'日';
}
function convertCurrency(currencyDigits) {
// Constants:
var MAXIMUM_NUMBER = 99999999999.99;
// Predefine the radix characters and currency symbols for output:
var CN_ZERO = "零";
var CN_ONE = "壹";
var CN_TWO = "贰";
var CN_THREE = "叁";
var CN_FOUR = "肆";
var CN_FIVE = "伍";
var CN_SIX = "陆";
var CN_SEVEN = "柒";
var CN_EIGHT = "捌";
var CN_NINE = "玖";
var CN_TEN = "拾";
var CN_HUNDRED = "佰";
var CN_THOUSAND = "仟";
var CN_TEN_THOUSAND = "万";
var CN_HUNDRED_MILLION = "亿";
var CN_SYMBOL = ""; //人民币
var CN_DOLLAR = "元";
var CN_TEN_CENT = "角";
var CN_CENT = "分";
var CN_INTEGER = "整";
// Variables:
var integral; // Represent integral part of digit number.
var decimal; // Represent decimal part of digit number.
var outputCharacters; // The output result.
var parts;
var digits, radices, bigRadices, decimals;
var zeroCount;
var i, p, d;
var quotient, modulus;
// Validate input string:
currencyDigits = currencyDigits.toString();
if (currencyDigits == "") {
alert("请输入小写金额!");
return "";
}
if (currencyDigits.match(/[^,.\d]/) != null) {
alert("小写金额含有无效字符!");
return "";
}
if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) {
alert("小写金额的格式不正确!");
return "";
}
// Normalize the format of input digits:
currencyDigits = currencyDigits.replace(/,/g, ""); // Remove comma delimiters.
currencyDigits = currencyDigits.replace(/^0+/, ""); // Trim zeros at the beginning.
// Assert the number is not greater than the maximum number.
if (Number(currencyDigits) > MAXIMUM_NUMBER) {
alert("金额过大,应小于1000亿元!");
return "";
}
// Process the coversion from currency digits to characters:
// Separate integral and decimal parts before processing coversion:
parts = currencyDigits.split(".");
if (parts.length > 1) {
integral = parts[0];
decimal = parts[1];
// Cut down redundant decimal digits that are after the second.
decimal = decimal.substr(0, 2);
}
else {
integral = parts[0];
decimal = "";
}
// Prepare the characters corresponding to the digits:
digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND);
bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
decimals = new Array(CN_TEN_CENT, CN_CENT);
// Start processing:
outputCharacters = "";
// Process integral part if it is larger than 0:
if (Number(integral) > 0) {
zeroCount = 0;
for (i = 0; i < integral.length; i++) {
p = integral.length - i - 1;
d = integral.substr(i, 1);
quotient = p / 4;
modulus = p % 4;
if (d == "0") {
zeroCount++;
}
else {
if (zeroCount > 0)
{
outputCharacters += digits[0];
}
zeroCount = 0;
outputCharacters += digits[Number(d)] + radices[modulus];
}
if (modulus == 0 && zeroCount < 4) {
outputCharacters += bigRadices[quotient];
zeroCount = 0;
}
}
outputCharacters += CN_DOLLAR;
}
// Process decimal part if there is:
if (decimal != "") {
for (i = 0; i < decimal.length; i++) {
d = decimal.substr(i, 1);
if (d != "0") {
outputCharacters += digits[Number(d)] + decimals[i];
}
}
}
// Confirm and return the final output string:
if (outputCharacters == "") {
outputCharacters = CN_ZERO + CN_DOLLAR;
}
if (decimal == "") {
outputCharacters += CN_INTEGER;
}
outputCharacters = CN_SYMBOL + outputCharacters;
return outputCharacters;
}
//获取大写
var money = convertCurrency($('#money').text());
$('#upper').text(money);
<?php
/**
* @author xiaoxia xu <x_824@sina.com> 2011-1-12
* @link http://www.phpwind.com
* @copyright Copyright © 2003-2110 phpwind.com
* @license
* 统计目录下的文件行数及总文件数··去除注释
*/
$fileExt = ".php";
$filePath = "D:\install\wamp\www\qshx\Lawyer\Home";
if (!is_dir($filePath)) exit('Path error!');
list($totalnum, $linenum, $filenum) = readGiveDir($filePath, $fileExt);
echo "*************************************************************\r\n";
echo "总行数: " . $totalnum . "\r\n";
echo "总有效行数: " . $linenum . "\r\n";
echo '总文件个数:' . $filenum;
function readGiveDir($dir, $fileExt) {
$totalnum = 0;
$linenum = 0;
$filenum = 0;
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (in_array($file, array(".", "..", '.svn'))) continue;
if (is_dir($dir . '/' . $file)) {
list($num1, $num2, $num3) = readGiveDir($dir . '/' . $file, $fileExt);
$totalnum += $num1;
$linenum += $num2;
$filenum += $num3;
} else {
if (strrchr($file, '.') == $fileExt) {
list($num1, $num2) = readfiles($dir . '/' . $file);
$totalnum += $num1;
$linenum += $num2;
$filenum ++;
continue;
}
}
}
closedir($dh);
} else {
echo 'open dir <' . $dir . '> error!' . "\r";
}
return array($totalnum, $linenum, $filenum);
}
function readfiles($file) {
echo $file . "\r\n";
//$p = php_strip_whitespace($file);
$str = file($file);
$linenum = 0;
foreach ($str as $value) {
if (skip(trim($value))) continue;
$linenum ++;
}
$totalnum = count(file($file));
echo '行数:' . $totalnum . "\r\n";
echo '有效行数:' . $linenum . "\r\n";
return array($totalnum, $linenum);
}
function skip($string) {
if ($string == '') return true;
$array = array("*", "/*", "//", "#");
foreach ($array as $tag) {
if (strpos($string, $tag) === 0) return true;
}
return false;
}
2011年7月,仅仅47岁的“中国第一程序员”求伯君彻底退隐江湖。这代表着一个时代的过去,在求伯君风光的年代,程序员身上充满着个人英雄主义的浪漫情怀。而随着时间的不断推移,单个程序员的能力显得越来越渺小,程序员逐渐沦为软件生产流水线上一颗螺丝钉,这让第一代程序员的神话再难重现。