php将颜色名称转换为16进制的表示形式。
代码:
/**
* 颜色值与16进制形式的转换
* edit: www.
*/
$trueColors = array(
'ANTIQUE WHITE' => '#FAEBD7',
'AQUA' => '#00FFFF',
'AQUAMARINE' => '#7FFFD4',
'AZURE' => '#F0FFFF',
'BEIGE' => '#F5F5DC',
'BISQUE' => '#FFE4C4',
'BLACK' => '#000000',
'BLANCHED ALMOND' => '#FFEBCD',
'BLUE' => '#0000FF',
'BLUE VIOLET' => '#8A2BE2',
'BROWN' => '#A52A2A',
'BURLY WOOD' => '#DEB887',
'CADET BLUE' => '#5F9EA0',
'CHARTREUSE' => '#7FFF00',
'CHOCOLATE' => '#D2691E',
'CORAL' => '#FF7F50',
'CORN FLOWER BLUE' => '#6495ED',
'CORN SILK' => '#FFF8DC',
'CRIMSON' => '#DC143C',
'CYAN' => '#00FFFF',
'DARK BLUE' => '#00008B',
'DARK CYAN' => '#008B8B',
'DARK GOLDENROD' => '#B8860B',
'DARK GRAY' => '#A9A9A9',
'DARK GREEN' => '#006400',
'DARK KHAKI' => '#BDB76B',
'DARK MAGENTA' => '#8B008B',
'DARK OLIVE GREEN' => '#556B2F',
'DARK ORANGE' => '#FF8C00',
'DARK ORCHID' => '#9932CC',
'DARK RED' => '#8B0000',
'DARK SALMON' => '#E9967A',
'DARK SEA GREEN' => '#8FBC8F',
'DARK SLATE BLUE' => '#483D8B',
'DARK SLATE GRAY' => '#2F4F4F',
'DARK TURQUOISE' => '#00CED1',
'DARK VIOLET' => '#9400D4',
'DEEP PINK' => '#FF1493',
'DEEP SKY BLUE' => '#00BFFF',
'DULL GRAY' => '#696969',
'DODGER BLUE' => '#1E90FF',
'FIRE BRICK' => '#B22222',
'FLORAL WHITE' => '#FFFAF0',
'FOREST GREEN' => '#228B22',
'FUCHSIA' => '#FF00FF',
'GAINSBORO' => '#DCDCDC',
'GHOST WHITE' => '#F8F8FF',
'GOLD' => '#FFD700',
'GOLDENROD' => '#DAA520',
'GRAY' => '#808080',
'GREEN' => '#008000',
'GREEN YELLOW' => '#ADFF2F',
'HONEYDEW' => '#F0FFF0',
'HOT PINK' => '#FF69B4',
'INDIAN RED' => '#CD5C5C',
'INDIGO' => '#4B0082',
'IVORY' => '#FFFFF0',
'KHAKI' => '#F0E68C',
'LAVENDER' => '#E6E6FA',
'LAVENDER BLUSH' => '#FFF0F5',
'LEAF' => ' #6B8E23',
'LEMON CHIFFON' => '#FFFACD',
'LIGHT BLUE' => '#ADD8E6',
'LIGHT CORAL' => '#F08080',
'LIGHT CYAN' => '#E0FFFF',
'LIGHT GOLDENROD YELLOW' => '#FAFAD2',
'LIGHT GREEN' => '#90EE90',
'LIGHT GRAY' => '#D3D3D3',
'LIGHT PINK' => '#FF86C1',
'LIGHT SALMON' => '#FFA07A',
'LIGHT SEA GREEN' => '#20B2AA',
'LIGHT SKY BLUE' => '#87CEFA',
'LIGHT STEEL BLUE' => '#778899',
'LIGHT YELLOW' => '#FFFFE0',
'LIME' => '#00FF00',
'LIME GREEN' => '#32CD32',
'LINEN' => '#FAF0E6',
'MAGENTA' => '#FF00FF',
'MAROON' => '#800000',
'MEDIUM AQUA MARINE' => '#66CDAA',
'MEDIUM BLUE' => '#0000CD',
'MEDIUM ORCHID' => '#BA55D3',
'MEDIUM PURPLE' => '#9370DB',
'MEDIUM SEA GREEN' => '#3CB371',
'MEDIUM SLATE BLUE' => '#7B68EE',
'MEDIUM SPRING BLUE' => '#00FA9A',
'MEDIUM TURQUOISE' => '#48D1CC',
'MEDIUM VIOLET RED' => '#C71585',
'MIDNIGHT BLUE' => '#191970',
'MINT CREAM' => '#F5FFFA',
'MISTY ROSE' => '#FFE4E1',
'NAVAJO WHITE' => '#FFDEAD',
'NAVY' => '#000080',
'OLD LACE' => '#FDF5E6',
'OLIVE' => '#808000',
'ORANGE' => '#FFA500',
'ORANGE RED' => '#FF4500',
'ORCHID' => '#DA70D6',
'PALE GOLDENROD' => '#EEE8AA',
'PALE GREEN' => '#98FB98',
'PALE TURQUOISE' => '#AFEEEE',
'PALE VIOLET RED' => '#DB7093',
'PAPAYAWHIP' => '#FFEFD5',
'PEACH PUFF' => '#FFDAB9',
'PERU' => '#CD853F',
'PINK' => '#FFC0CB',
'PLUM' => '#DDA0DD',
'POWDER BLUE' => '#B0E0E6',
'PURPLE' => '#800080',
'RED' => '#FF0000',
'ROSY BROWN' => '#BC8F8F',
'ROYAL BLUE' => '#4169E1',
'SADDLE BROWN' => '#8B4513',
'SALMON' => '#FA8072',
'SANDY BROWN' => '#F4A460',
'SEA GREEN' => '#2E8B57',
'SEASHELL' => '#FFF5EE',
'SIENNA' => '#A0522D',
'SILVER' => '#C0C0C0',
'SKY BLUE' => '#87CEEB',
'SLATE BLUE' => '#6A5ACD',
'SLATE GRAY' => '#708090',
'SNOW' => '#FFFAFA',
'SPRING GREEN' => '#00FF7F',
'STEEL BLUE' => '#4682B4',
'TAN' => '#D2B48C',
'TEAL' => '#008080',
'THISTLE' => '#D88FD8',
'TOMATO' => '#FF6347',
'TURQUOISE' => '#40E0D0',
'VIOLET' => '#EE82EE',
'WHEAT' => '#F5DEB3',
'WHITE' => '#FFFFFF',
'WHITE SMOKE' => '#F5F5F5',
'YELLOW' => '#FFFF00',
'YELLOW GREEN' => '#9ACD32');
?>
本节给出的php代码,实现了二个函数,用于将颜色值转换为16进制数字,或将16进制数字转换为颜色值。
例子:
<?php
/**
* 颜色值与16进制数字的转换
* edit: WWW.JBXUE.COM
*/
function toHex($N) {
if ($N==NULL) return "00";
if ($N==0) return "00";
$N=max(0,$N);
$N=min($N,255);
$N=round($N);
$string = "0123456789ABCDEF";
$val = (($N-$N%16)/16);
$s1 = $string{$val};
$val = ($N%16);
$s2 = $string{$val};
return $s1.$s2;
}
//颜色值转换为16进制数字
function rgb2hex($r,$g,$b){
return toHex($r).toHex($g).toHex($b);
}
//16进制数字转换为颜色值
function hex2rgb($N){
$dou = str_split($N,2);
return array(
"R" => hexdec($dou[0]),
"G" => hexdec($dou[1]),
"B" => hexdec($dou[2])
);
}
echo rgb2hex(106,48,48);
echo '<p> </p>';
print_r(hex2rgb("6A3030"));
?>
php导入csv到MySql数据库。
代码:
<?php
/**
* 将csv文件导入到mysql数据库
* edit: www.
*/
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername ="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "output.sql";
/********************************/
if (!file_exists($csvfile)) {
echo "File not found. Make sure you specified the correct path.\n";
exit;
}
$file = fopen($csvfile,"r");
if (!$file) {
echo "Error opening data file.\n";
exit;
}
$size = filesize($csvfile);
if (!$size) {
echo "File is empty.\n";
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$con = @mysql_connect()($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());
$lines = 0;
$queries = "";
$linearray = array();
foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace()("\r","",$line);
/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
$line = str_replace("'","\'",$line);
/*************************************/
$linearray = explode()($fieldseparator,$line);
$linemysql = implode("','",$linearray);
if($addauto)
$query = "insert into $databasetable values('','$linemysql');";
else
$query = "insert into $databasetable values('$linemysql');";
$queries .= $query . "\n";
@mysql_query()($query);
}
@mysql_close($con);
if ($save) {
if (!is_writable($outputfile)) {
echo "File is not writable, check permissions.\n";
}
else {
$file2 = fopen($outputfile,"w");
if(!$file2) {
echo "Error writing to the output file.\n";
}
else {
fwrite($file2,$queries);
fclose($file2);
}
}
}
echo "在csv文件中共找到多少 $lines 条记录.\n";
?>