php 汉字转成拼音代码
<?php
function utf82gb($utfstr){ // UTF-8汉字转GBK汉字
if(function_exists('iconv')){ return iconv('utf-8','gbk',$utfstr); }
global $UC2GBTABLE;
$okstr = "";
if(trim($utfstr)=="") return $utfstr;
if(empty($UC2GBTABLE)){
$filename = dirname(__FILE__) . "/gb2312-utf8.table";
$fp = fopen($filename,"r");
while($l = fgets($fp,15))
{ $UC2GBTABLE[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));}
fclose($fp);
}
$okstr = "";
$ulen = strlen($utfstr);
for($i=0;$i<$ulen;$i++)
{
$c = $utfstr[$i];
$cb = decbin(ord($utfstr[$i]));
if(strlen($cb)==8){
$csize = strpos(decbin(ord($cb)),"0");
for($j=0;$j < $csize;$j++){
$i++; $c .= $utfstr[$i];
}
$c = utf82u($c);
if(isset($UC2GBTABLE[$c])){
$c = dechex($UC2GBTABLE[$c]+0x8080);
$okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3]));
}
else
{ $okstr .= "&#".$c.";";}
}
else $okstr .= $c;
}
$okstr = trim($okstr);
return $okstr;
}
function SpGetPinyin($str,$ishead=0,$isclose=1){ // GBK汉字转拼音
global $pinyins;
$restr = "";
$str = trim($str);
$slen = strlen($str);
if($slen<2) return $str;
if(count($pinyins)==0){
$fp = fopen(dirname(__FILE__) . '/pinyin.db',"r");
while(!feof($fp)){
$line = trim(fgets($fp));
$pinyins[$line[0].$line[1]] = substr($line,3,strlen($line)-3);
}
fclose($fp);
}
for($i=0;$i<$slen;$i++){
if(ord($str[$i])>0x80)
{
$c = $str[$i].$str[$i+1];
$i++;
if(isset($pinyins[$c])){
if($ishead==0) $restr .= $pinyins[$c];
else $restr .= $pinyins[$c][0];
}else $restr .= "_";
}else if( eregi("[a-z0-9]",$str[$i]) ){ $restr .= $str[$i]; }
else{ $restr .= "_"; }
}
if($isclose==0) unset($pinyins);
return $restr;
}
function getPinyin($string){ // UTF-8汉字转拼音
return SpGetPinyin(utf82gb($string));
}
echo getPinyin("中国WEB第一站");
?>
php 删除数据表与删除数据库
//---------------------------删除数据表
function Ebak_Drop($tablename,$dbname){
global $empire;
$dbname=RepPostVar($dbname);
$empire->query("use `$dbname`");
$count=count($tablename);
if(empty($count))
{printerror("EmptyChangeDelTb","history.go(-1)");}
$a="";
$first=1;
for($i=0;$i<$count;$i++)
{
if(empty($first))
{
$a.=",";
}
else
{
$first=0;
}
$a.="`".$tablename[$i]."`";
}
$sql1=$empire->query("DROP TABLE IF EXISTS ".$a.";");
printerror("DelTbSuccess","ChangeTable.php?mydbname=$dbname");
}
//--------------------------删除数据库
function Ebak_DropDb($dbname){
global $empire;
$dbname=RepPostVar($dbname);
if(empty($dbname))
{
printerror("NotChangeDelDb","history.go(-1)");
}
$sql=$empire->query("DROP DATABASE `$dbname`");
if($sql)
{
printerror("DelDbSuccess","ChangeDb.php");
}
else
{printerror("DbError","history.go(-1)");}
}
php实例多文件上传实例代码
//a = $('<span class="MultiFile-title" title="'+MultiFile.STRING.selected.replace('$file', v)+'">'+MultiFile.STRING.file.replace('$file', v.match(/[^/\]+$/gi)[0])+'</span>'),
//这是新的代码结构.
//取出图片格式.
ext = String(v.match(/.w{1,4}$/gi));
// v变量是路径地址, 所以可以直接用img来显示出来.
var imgs = (ext=='.jpg' || ext=='.gif' || ext=='.png')?'<img onmouseover="alert(this.src)" width="30"" width=100% src="'+v+'"/>':'';
// 从'+imgs+'开始,就是我增加的.
a = $('<span class="MultiFile-title" title="'+MultiFile.STRING.selected.replace('$file', v)+'">'+MultiFile.STRING.file.replace('$file', v.match(/[^/\]+$/gi)[0])+'</span> '+imgs+' <a class="a_b" href="javascript:;" title="点击插入主题" rsok="'+slave_count+'" onclick="alert('+slave_count+'); return false">点击插入</a>'),
b = $('<a class="MultiFile-remove" href="#'+MultiFile.wrapID+'">'+MultiFile.STRING.remove+'</a>');
复制代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script type="text/javascript"" width=100% src="jquery.js"></script>
<script type="text/javascript"" width=100% src="jquery.MultiFile.js"></script>
<script type="text/javascript">
<!--
$(function (){
//测试jquery是否加载
//alert('jquery');
//用示例7模式的内容
$('#T7').MultiFile({
list: '#T7-list'
});
})
-->
</script>
<body style="font-size:14px; margin:10%; height:">
<!--建立一个ID #T7 相应的设置一个div为T7-list来显示内容-->
<input id="T7" name="upfile" type="file" />
<div id="T7-list"></div>
<div>部分测试: <a href="javascript:;" onclick="alert($('#T7-list').html())">列表内容</a></div>
</body>
</html>
复制代码 //对于编辑来说, 键名key就会从0开始计算.所以获取一下附件最后一个rsok值是非常重要的.
//var int_s=$('.a_b:last').attr('rsok');
// Bind functionality to the first element
if(!MultiFile.MultiFile) MultiFile.addSlave(MultiFile.e, 0);
复制代码
//正则(这个还不是)
$reg = "/<img[^>]*src="(http://(.+)/(.+).(jpg|gif|bmp|bnp))"/isU";
//把抠出来的 img 地址存放到 $img_array 变量中
preg_match_all($reg, $message, $img_array, PREG_PATTERN_ORDER);
//过滤重复的图片
$img_array = array_unique($img_array[1]);
第二步. 把$img_array 数组循环一下. 做图片保存和文章位置替换
//判断是否是自己网站上的 图片
if('xxx.com' != get_domain($img)){// 如果这个图片不是自己服务器上的
//读取图片文件
$Gimg = new GetImage();
$Gimg->source = $img;
$Gimg->save_to = './data/temp/';
$FILE = $Gimg->download(); //图片移动到本地
//保存到相册 得到图片保存的位置
$img_path = pic_save($FILE,0,'');
//文本路径替换
$message = str_replace($img, $img_path, $message);
}
}
....这时候 $message 里面已经图片已经替换为自己服务器本地的地址,并且图片也保存到自己的服务器上.
//从url中获得域名
function get_domain($url){
$pattern = "/[w-]+.(com|net|org|gov|cc|biz|info|cn)(.(cn|hk))*/";
preg_match($pattern, $url, $matches);
if(count($matches) > 0) {
return $matches[0];
}else{
$rs = parse_url($url);
$main_url = $rs["host"];
if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
return $main_url;
}else{
$arr = explode(".",$main_url);
$count=count($arr);
$endArr = array("com","net","org","3322");//com.cn net.cn 等情况
if (in_array($arr[$count-2],$endArr)){
$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
}else{
$domain = $arr[$count-2].".".$arr[$count-1];
}
return $domain;
}// end if(!strcmp...)
}// end if(count...)
}// end function
// 从远程吧图片载到服务器本地 的 类
class GetImage {
var $source;
var $save_to;
var $quality;
function download($method = 'curl') {
$info = @GetImageSize($this->source);
$mime = $info['mime'];
// What sort of image?
$type = substr(strrchr($mime, '/'), 1);
switch ($type){
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
// Best Quality: 100
$quality = isSet($this->quality) ? $this->quality : 100;
break;
case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png';
// Compression Level: from 0 (no compression) to 9
$quality = isSet($this->quality) ? $this->quality : 0;
break;
case 'bmp':
$image_create_func = 'ImageCreateFromBMP';
$image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
break;
case 'gif':
$image_create_func = 'ImageCreateFromGIF';
$image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
break;
case 'vnd.wap.wbmp':
$image_create_func = 'ImageCreateFromWBMP';
$image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
break;
case 'xbm':
$image_create_func = 'ImageCreateFromXBM';
$image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
break;
default:
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
}
if(isSet($this->set_extension)){
$ext = strrchr($this->source, ".");
$strlen = strlen($ext);
$new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext;
}else{
$new_name = basename($this->source);
}
$save_to = $this->save_to."/blog_insert_temp_".time().mt_rand(1,99).".".$new_image_ext;
//输出对象 组成跟$_FILE变量一样 得到后自己和平常图片上传处理一样了
$img_info['name'] = basename($this->source);
$img_info['type'] = $mime;
$img_info['size'] = 1000;
$img_info['tmp_name'] = $save_to;
$img_info['error'] = 0;
if($method == 'curl'){
$save_image = $this->LoadImageCURL($save_to);
}elseif($method == 'gd'){
$img = $image_create_func($this->source);
if(isSet($quality)){
$save_image = $image_save_func($img, $save_to, $quality);
}else{
$save_image = $image_save_func($img, $save_to);
}
}
return $img_info;
}
function LoadImageCURL($save_to){
$ch = curl_init($this->source);
$fp = fopen($save_to, "wb");
// set URL and other appropriate options
$options = array(CURLOPT_FILE => $fp,
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough)
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
}
class upload_class{
private $ptname; //上传表单名称;
private $udname; //是否以月份建立子目录(0为否,其他为真);
private $ufname; //是否以时间建立文件名(0为否,其他为真);
private $ultype; //上传文件类型;
private $ulsize; //上传文件大小;
private $ulname; //输出文件名称;
private $ulpath; //输出文件路径;
private $wm; //水印附加(0为不加,其他为加);
private $wmtype; //水印类型(0为文字,其他为图片);
private $wmpic; //水印图片;
private $wmpicquality; //图片质量;
private $wmpictrans; //图片透明;
private $wmstr; //水印字符;
private $wmstrsize; //字符大小;
private $wmstrfont; //字符字体;
private $wmstrcolor; //字符颜色;
private $wmpos; //水印位置(1为顶端居左,2为顶端居中,3为顶端居右……);
function __construct($ptname='upfile',$udname=1,$ufname=1,$ultype=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'),$wm=1,$wmtype=1,$wmpic='images/wm.gif',$ulsize=2097152,$ulpath='images/temp/',$wmpictrans=20,$wmpicquality=80,$wmstr='DONLINE',$wmstrsize=5,$wmstrfont='./font/cour.ttf',$wmstrcolor='#ff0000',$wmpos=9){
$this->ptname=$_FILES[$ptname];
$this->udname=$udname;
$this->ufname=$ufname;
$this->ultype=$ultype;
$this->ulsize=$ulsize;
$this->ulpath=$ulpath;
$this->wm=$wm;
$this->wmtype=$wmtype;
$this->wmpic=$wmpic;
$this->wmpicquality=$wmpicquality;
$this->wmpictrans=$wmpictrans;
$this->wmstr=$wmstr;
$this->wmstrsize=$wmstrsize;
$this->wmstrfont=$wmstrfont;
$this->wmstrcolor=$wmstrcolor;
$this->wmpos=$wmpos;
}
function uploadfun(){
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!is_uploaded_file($this->ptname['tmp_name']))$this->errorfun('上传失败!');
if(!in_array($this->ptname['type'],$this->ultype))$this->errorfun('不支持的文件类型!');
if($this->ulsize<$this->ptname['size'])$this->errorfun('文件太大!');
if($this->udname){date_default_timezone_set('UTC');$this->ulpath=$this->ulpath.'month_'.date('Ym').'/';}
else{$this->ulpath=$this->ulpath;}
$this->createfun($this->ulpath);
if($this->ufname){$t=pathinfo($this->ptname['name']);$this->ulname=$this->ulpath.time().'.'.$t['extension'];}
else{$this->ulname=$this->ulpath.$this->ptname['name'];}
if(file_exists($this->ulname))$this->errorfun('该文件已存在!');
if(!move_uploaded_file($this->ptname['tmp_name'],$this->ulname))$this->errorfun('移动文件错误!');
$this->wmfun();
$this->errorfun('上传成功!');
}
}
function createfun($d){
if(!file_exists($d)){$this->createfun(dirname($d));mkdir($d);}
}
function wmfun(){
if($this->wm){
if(file_exists($this->ulname)){
$groundimg=getimagesize($this->ulname);
$ow=$groundimg[0];
$oh=$groundimg[1];
switch($groundimg[2]){
case 1:$g=imagecreatefromgif($this->ulname);break;
case 2:$g=imagecreatefromjpeg($this->ulname);break;
case 3:$g=imagecreatefrompng($this->ulname);break;
case 4:$g=imagecreatefromwbmp($this->ulname);break;
default:$this->errorfun('不支持的背景图片类型!');
}
}
else{$this->errorfun('背景图片不存在!');}
if(file_exists($this->wmpic)){
$wmimg=getimagesize($this->wmpic);
$ww=$wmimg[0];
$wh=$wmimg[1];
switch($wmimg[2]){
case 1:$w=imagecreatefromgif($this->wmpic);break;
case 2:$w=imagecreatefromjpeg($this->wmpic);break;
case 3:$w=imagecreatefrompng($this->wmpic);break;
case 4:$w=imagecreatefromwbmp($this->wmpic);break;
default:$this->errorfun('不支持的水印图片类型!');
}
}
else{$this->errorfun('水印图片不存在!');}
switch($this->wmtype){
case 0:$tp=imagettfbbox(ceil($this->wmstrsize*2.5),0,$this->wmstrfont,$this->wmstr);$ww=$tp[2]-$tp[6];$wh=$tp[3]-$tp[7];unset($tp);break;
case 1:$ww=$ww;$wh=$wh;break;
default:$ww=$ww;$wh=$wh;break;
}
if($ow<$ww || $oh<$wh)$this->errorfun('背景图片太小!无法生成水印!');
switch($this->wmpos){
case 0:$x=rand(0,($ow-$ww));$y=rand(0,($oh-$wh));break;//随机
case 1:$x=0;$y=0;break;//1为顶端居左
case 2:$x=($ow-$ww)/2;$y=0;break;//2为顶端居中
case 3:$x=$ow-$ww;$y=0;break;//3为顶端居右
case 4:$x=0;$y=($oh-$wh)/2;break;//4为中部居左
case 5:$x=($ow-$ww)/2;$y=($oh-$wh)/2;break;//5为中部居中
case 6:$x=$ow-$ww;$y=($oh-$wh)/2;break;//6为中部居右
case 7:$x=0;$y=$oh-$wh;break;//7为底端居左
case 8:$x=($ow-$ww)/2;$y=$oh-$wh;break;//8为底端居中
case 9:$x=$ow-$ww;$y=$oh-$wh;break;//9为底端居右
default:$x=rand(0,($ow-$ww));$y=rand(0,($oh-$wh));break;//随机
}
imagealphablending($g, true);
switch($this->wmtype){
case 0:
if($this->wmstrcolor){$R=hexdec(substr($this->wmstrcolor,1,2));$G=hexdec(substr($this->wmstrcolor,3,2));$B=hexdec(substr($this->wmstrcolor,5));}
else{$this->errorfun('水印文字颜色不存在!');}
imagestring($g,$this->wmstrfont,$x,$y,$this->wmstr,imagecolorallocate($g,$R,$G,$B));break;
case 1;imagecopymerge($g,$w,$x,$y,0,0,$ww,$wh,$this->wmpictrans);break;
default:imagecopymerge($g,$w,$x,$y,0,0,$ww,$wh,$this->wmpictrans);break;
}
@unlink($this->ulname);
switch($groundimg[2]){
case 1:imagegif($g,$this->ulname);break;
case 2:imagejpeg($g,$this->ulname,$this->wmpicquality);break;
case 3:imagepng($g,$this->ulname);break;
case 4:imagewbmp($g,$this->ulname);break;
default:$this->errorfun('生成图片失败!');
}
if(isset($wmimg))unset($wmimg);
if(isset($w))imagedestroy($w);
unset($groundimg);
imagedestroy($g);
}
}
function errorfun($e='未知错误!'){
$msg='<script language="javascript" type="text/javascript">';
$msg.='alert("'.$e.'");';
$msg.='history.back();';
$msg.='</script>';
echo $msg;
exit;
}
}
?>