编辑通过excel表格修改了大批的产品价格和促销时间,让我们技术批量导入到线上数据库。
这样对于我们来说是一件在简单不过的事情了,保护phpexcel导表利器,瞬间解决问题。
可是,进入数据库一看:蒙了,导入的时间格式有问题,展示的不是时间,是数字,郁闷中。
然后通过php输出,果然不是时间的格式。
百度一遍发现,phpexcel里面提供了这样的方法getFormattedValue()来读出时间的,将getValue()换成
getFormattedValue();
$abc = $currentSheet->getCell ( ‘A’ . $currentRow )->getFormattedValue ();
这样就可以顺利的读出excel表里的时间,重新更新数据库,OK。
下面看个例子
error_reporting(E_ALL);
date_default_timezone_set('Asia/shanghai');
/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';
$inputFileName = '6081076641077444758.xls';
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load($inputFileName);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$tempArray = array();
for($j=2;$j<=$highestRow;$j++){
for($k='A';$k<=$highestColumn;$k++){
if($k=='M'||$k=='O') //M列和O列是时间
$tempArray[] = excelTime($objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue());
else
$tempArray[] = $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
}
print_r($tempArray);
unset($tempArray);
}
function excelTime($date, $time = false) {
if(function_exists('GregorianToJD')){
if (is_numeric( $date )) {
$jd = GregorianToJD( 1, 1, 1970 );
$gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
$date = explode( '/', $gregorian );
$date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
. ($time ? " 00:00:00" : '');
return $date_str;
}
}else{
$date=$date>25568?$date+1:25569;
/*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
$ofs=(70 * 365 + 17+2) * 86400;
$date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
}
return $date;
}
重定向是一个看似简单,实际很复杂的问题。HTTP状态码一共才五个系列(1XX,2XX,3XX,4XX,5XX),而重定向状态码被单独作为一个系列(3XX)存在,足以说明它的重要性,但是很多人认为知道301/302的区别就算熟悉重定向了,这实在是一大谬误。本文主要介绍一下303/307。
为了方便测试,首先熟悉一下如何使用curl命令得到响应头:
-I/–head
(HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document. When used on a FTP or FILE file, curl displays the file size and last modification time only.
例子命令:curl -I http://www.google.com/
通常PHP里的重定向是这样的:
header('Location: http://localhost/');
exit();
通过curl命令访问如上的代码,我们将得到如下响应头:
HTTP/1.1 302 Found
Location: http://localhost/
如上的重定向代码在编程里很常用,比如说添加文章成功后,跳转回列表页,不过这里的重定向状态码302却是值得商榷的。
这还得从头说起,在HTTP1.0的时代,那时候302的名字还是“Moved Temporarily”,但在实际使用上,302往往包含了两方面的意思(也就是后来的303/307),为了消除可能的混淆,在HTTP1.1中,302被重命名为“Found”,并新加了303(See Other)和307(Temporary Redirect),至于PHP之所以在重定向时缺省使用302状态码是为了兼容的目的,所以不到不得已(有时候,客户端是HTTP1.0的,只理解302),不应该使用302。
303和307都把重定向的URI置于Location头中,他们的区别在于:
303:对于POST请求,它表示请求已经被处理,客户端可以接着使用GET方法去请求Location里的URI。
307:对于POST请求,表示请求还没有被处理,客户端应该向Location里的URI重新发起POST请求。
下面看看PHP如何发送非302的重定向,以303为例:
// 第一种方法
header('Location: http://localhost/', true, 303);
// 第二种方法
header('HTTP/1.1 303 See Other');
header('Location: http://localhost/');
使用curl命令,你就会看到如下响应头:
HTTP/1.1 303 See Other
Location: http://localhost/
总结,本文说的主要是303/307之间的关系。之所以明确区分是为了让状态码本身能够准确的表达响应的含义,从而尽可能的避免对重定向的滥用。
最后在把网站编辑器由以前的fck改成UEDITOR编辑器了,但是发现UEDITOR编辑器上传图片是自动提取的,但是图片没有水印功能,下面小编和各位一起来看看。
UEditor编辑器没有上传图片加水印的功能,需要进行二次开发,本例是在PHPCMS系统中对百度编辑器进行二次开发,添加上传图片加水印功能。
首先打开UEditor编辑器文件目录的php文件夹,打开Uploader.class.php,把PHPCMS添加水印的方法复制过来,加到这个类所有成员方法最后面,然后进行修改如下:
//图片加水印
public function watermark($source, $target = '', $w_pos = '', $w_img = '', $w_text = '99danji',$w_font = 8, $w_color = '#ff0000') {
$this->w_img = 'watermark.png';
$this->w_pos = 9;
$this->w_minwidth = 400;
$this->w_minheight = 200;
$this->w_quality = 80;
$this->w_pct = 85;
$w_pos = $w_pos ? $w_pos : $this->w_pos;
$w_img = $w_img ? $w_img : $this->w_img;
//if(!$this->watermark_enable || !$this->check($source)) return false;
if(!$target) $target = $source;
//$w_img = PHPCMS_PATH.$w_img;
//define('WWW_PATH', dirname(dirname(dirname(__FILE__)));
$w_img = '../../../images/water/'.$w_img;
$source_info = getimagesize($source);
$source_w = $source_info[0];
$source_h = $source_info[1];
//if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false;
switch($source_info[2]) {
case 1 :
$source_img = imagecreatefromgif($source);
break;
case 2 :
$source_img = imagecreatefromjpeg($source);
break;
case 3 :
$source_img = imagecreatefrompng($source);
break;
default :
return false;
}
if(!empty($w_img) && file_exists($w_img)) {
$ifwaterimage = 1;
$water_info = getimagesize($w_img);
$width = $water_info[0];
$height = $water_info[1];
switch($water_info[2]) {
case 1 :
$water_img = imagecreatefromgif($w_img);
break;
case 2 :
$water_img = imagecreatefromjpeg($w_img);
break;
case 3 :
$water_img = imagecreatefrompng($w_img);
break;
default :
return;
}
} else {
$ifwaterimage = 0;
$temp = imagettfbbox(ceil($w_font*2.5), 0, PC_PATH.'libs/data/font/elephant.ttf', $w_text);
$width = $temp[2] - $temp[6];
$height = $temp[3] - $temp[7];
unset($temp);
}
switch($w_pos) {
case 1:
$wx = 5;
$wy = 5;
break;
case 2:
$wx = ($source_w - $width) / 2;
$wy = 0;
break;
case 3:
$wx = $source_w - $width;
$wy = 0;
break;
case 4:
$wx = 0;
$wy = ($source_h - $height) / 2;
break;
case 5:
$wx = ($source_w - $width) / 2;
$wy = ($source_h - $height) / 2;
break;
case 6:
$wx = $source_w - $width;
$wy = ($source_h - $height) / 2;
break;
case 7:
$wx = 0;
$wy = $source_h - $height;
break;
case 8:
$wx = ($source_w - $width) / 2;
$wy = $source_h - $height;
break;
case 9:
$wx = $source_w - $width;
$wy = $source_h - $height;
break;
case 10:
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
default:
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
}
if($ifwaterimage) {
if($water_info[2] == 3) {
imagecopy($source_img, $water_img, $wx, $wy, 0, 0, $width, $height);
} else {
imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
}
} else {
if(!empty($w_color) && (strlen($w_color)==7)) {
$r = hexdec(substr($w_color,1,2));
$g = hexdec(substr($w_color,3,2));
$b = hexdec(substr($w_color,5));
} else {
return;
}
imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
}
switch($source_info[2]) {
case 1 :
imagegif($source_img, $target);
break;
case 2 :
imagejpeg($source_img, $target, $this->w_quality);
break;
case 3 :
imagepng($source_img, $target);
break;
default :
return;
}
if(isset($water_info)) {
unset($water_info);
}
if(isset($water_img)) {
imagedestroy($water_img);
}
unset($source_info);
imagedestroy($source_img);
return true;
}
public function check($image) {
return extension_loaded('gd') && preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) && file_exists($image) && function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
}
对比我修改的部分,www.111cn.net由于phpcms水印可以在后台管理设置,phpcms自带的水印方法通过读取配置文件获取路径,和读取数据库设置获取参数设置,那么这些地方需要手动进行设置。
对了,在upFile方法还要添加一段函数:
if ($this->watermark) {
$this->watermark($this->filePath,$this->filePath);
}
然后打开UEditor百度编辑器php目录下的action_upload.php文件,加上是否添加水印的参数:
/* 上传配置 */
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
case 'uploadimage':
$config = array(
"pathFormat" => $CONFIG['imagePathFormat'],
"maxSize" => $CONFIG['imageMaxSize'],
"allowFiles" => $CONFIG['imageAllowFiles']
);
$fieldName = $CONFIG['imageFieldName'];
$watermark = true;
break;
然后在后面还有一句要改成:
/* 生成上传实例对象并完成上传 */
$up = new Uploader($fieldName, $config, $base64, $watermark);
这样就大功告成了,本文主要是提供思路和参考。
在php中对于简单的数据排序为我们提供了sort、asort、rsort、krsort、ksort函数了,下文我来给各位简单的介绍sort、asort、rsort、krsort、ksort函数的区别了。在php中自带了大量了数组排序函数,下面我们一一来介绍一下关于php数组排序的用法吧。
sort() 函数用于对数组单元从低到高进行排序。
rsort() 函数用于对数组单元从高到低进行排序。
asort() 函数用于对数组单元从低到高进行排序并保持索引关系。
arsort() 函数用于对数组单元从高到低进行排序并保持索引关系。
ksort() 函数用于对数组单元按照键名从低到高进行排序。
krsort() 函数用于对数组单元按照键名从高到低进行排序。
sort()
PHP sort() 函数用于对数组单元从低到高进行排序,如果成功则返回 TRUE,失败则返回 FALSE。
注意:本函数会为排序的数组中的单元赋予新的键名,这将删除原有的键名而不仅是重新排序。
语法:
bool sort( array &array [, int sort_flags] )
可选参数 sort_flags 用于改变排序的行为:
sort_flags 取值 说明
SORT_REGULAR 正常比较单元
SORT_NUMERIC 单元被作为数字来比较
SORT_STRING 单元被作为字符串来比较
SORT_LOCALE_STRING 根据当前的区域(locale)设置来把单元当作字符串比较
例子:
$arr = array("b", "a", "c"); sort($arr); print_r($arr); ?>
Array ( [0] => a [1] => b [2] => c )
在本例中, $arr 数组单元被按照字母顺序排序,而数组单元经过排序后,键值重新分配。
rsort()
PHP rsort() 函数行为与 sort() 相反,对数组单元进行由高到低排序,请参考 sort() 函数使用。
asort()
PHP asort() 函数用于对数组单元从低到高进行排序并保持索引关系,如果成功则返回 TRUE,失败则返回 FALSE。
语法:
bool asort( array &array [, int sort_flags] )
可选参数 sort_flags 用以改变排序的行为,具体参见 sort()。
例子:
$arr = array("b", "a", "c"); asort($arr); print_r($arr); ?>
运行该例子输出:
Array ( [1] => a [0] => b [2] => c )
arsort()
PHP arsort() 函数行为与 asort() 相反,对数组单元进行由高到低排序并保持索引关系,请参考 asort() 函数使用。
ksort()
PHP ksort() 函数用于对数组单元按照键名从低到高进行排序,如果成功则返回 TRUE,失败则返回 FALSE。
本函数会保留原来的键名,因此常用于关联数组。
语法:
bool ksort( array &array [, int sort_flags] )
可选参数 sort_flags 用以改变排序的行为,具体参见 sort()。
例子:
$arr = array("b"=>18, "a"=>20, "c"=>25); ksort($arr); print_r($arr); ?>
运行该例子输出:
Array ( [a] => 20 [b] => 18 => 25 )
krsort()
PHP krsort() 函数行为与 ksort() 相反,对数组单元按照键名进行由高到低排序,请参考 ksort() 函数使用
100万条记录的文本文件,取出重复数最多的前10条。
示例文本:
098
123
234
789
……
234
678
654
123
$fp = fopen('文件', 'r'); while($buf = fgets($fp)) { $res[$buf]++; } fclose($fp); arsort($res); $res = array_keys(array_slice($res, 0, 10)); print_r($res);
$a = file('文件'); $res = array_count_values($a); arsort($res); $res = array_keys(array_slice($res, 0, 10)); print_r($res);