收集一些实用的小函数

收集一些实用的小函数

作者:admin 时间:2007-07-20 分类:PHP 评论:0条 浏览:1677

看了一些程序,觉得很多php小函数挺强悍的,搜集一下,留个记号.
给字符(数组)添加slashes:
[coolcode lang=”php”]
function daddslashes($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val);
}
} else {
$string = addslashes($string);
}
return $string;
}
[/coolcode]

程序执行后,页面跳转:
[coolcode lang=”php”]
function continue_redirect($extra = ”) {
global $scriptname, $step, $end, $stay, $convertedrows, $totalrows, $maxid;
$url = $scriptname.”?step=$step&start=”.($end + 1).”&stay=$stay
&totalrows=$totalrows&convertedrows=$convertedrows&maxid=$maxid”.$extra;
echo “\r\n”;
echo “

[停止运行]

如果您的浏览器没有自动跳转,请点击这里

“;
}
[/coolcode]
产生随机数字和字母:
[coolcode lang=”php”]
function random($length, $numeric = 0) {
mt_srand((double)microtime() * 1000000);
if($numeric) {
$hash = sprintf(‘%0′.$length.’d’, mt_rand(0, pow(10, $length) – 1));
} else {
$hash = ”;
$chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz’;
$max = strlen($chars) – 1;
for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } } return $hash; } [/coolcode] 数据库错误中文提示 [coolcode lang="php"] function mysqlerror() { $mysqlerrno = mysql_errno(); switch($mysqlerrno) { case '1005': $errmsg = "创建表失败"; break; case '1006': $errmsg = "创建数据库失败"; break; case '1007': $errmsg = "数据库已存在,创建数据库失败"; break; case '1008': $errmsg = "数据库不存在,删除数据库失败"; break; case '1016': $errmsg = "无法打开数据文件"; break; case '1041': $errmsg = "系统内存不足"; break; case '1045': $errmsg = "连接数据库失败,用户名或密码错误"; break; case '1044': $errmsg = "当前用户没有访问数据库的权限"; break; case '1048': $errmsg = "字段不能为空"; break; case '1049': $errmsg = "数据库不存在"; break; case '1050': $errmsg = "数据表已存在"; break; case '1051': $errmsg = "数据表不存在"; break; case '1054': $errmsg = "字段不存在"; break; case '1062': $errmsg = "字段值重复,入库失败"; break; case '1064': $errmsg = "可能原因:1.数据超长或类型不匹配;2.数据库记录重复"; break; case '1065': $errmsg = "无效的SQL语句,SQL语句为空"; break; case '1081': $errmsg = "不能建立Socket连接"; break; case '1129': $errmsg = "数据库出现异常,请重启数据库"; break; case '1130': $errmsg = "连接数据库失败,没有连接数据库的权限"; break; case '1133': $errmsg = "数据库用户不存在"; break; case '1141': $errmsg = "当前用户无权访问数据库"; break; case '1142': $errmsg = "当前用户无权访问数据表"; break; case '1143': $errmsg = "当前用户无权访问数据表中的字段"; break; case '1146': $errmsg = "数据表不存在"; break; case '1149': $errmsg = "SQL语句语法错误"; break; case '1169': $errmsg = "字段值重复,更新记录失败"; break; case '2003': $errmsg = "请检查数据库服务器端口设置是否正确,默认端口为 3306"; break; case '2005': $errmsg = "数据库服务器不存在"; break; } return $mysqlerrno ? '
Error ‘.$mysqlerrno.’ : ‘.mysql_error().’.
错误 ‘.$mysqlerrno.’:’.$errmsg.’。
‘ : ”;
}
[/coolcode]
mysql查询调试开关
[coolcode lang=”php”]
function mysqlquery($sql) {
return $GLOBALS[‘debug’] ? mysql_query($sql) : @mysql_query($sql);
}
[/coolcode]
根据扩展名得到不同文件的MIME类型
[coolcode lang=”php”]
function getfiletype($filename = ”) {
$name = explode(“.”,$filename);
$exts = isset($name[1]) ? strtolower($name[1]) : ”;
switch ($exts) {
case ‘jpg’:
return ‘image/pjpeg’;
break;
case ‘jpe’:
return ‘image/pjpeg’;
break;
case ‘jpeg’:
return ‘image/pjpeg’;
break;
case ‘pdf’:
return ‘application/pdf’;
break;
case ‘gif’:
return ‘image/gif’;
break;
case ‘bmp’:
return ‘image/bmp’;
break;
case ‘png’:
return ‘image/png’;
break;
case ‘rar’:
return ‘x-rar-compressed’;
break;
case ‘txt’:
return ‘text/plain’;
break;
case ‘swf’:
return ‘application/x-shockwave-flash’;
break;
case ‘zip’:
return ‘application/zip’;
break;
case ‘doc’:
return ‘application/msword’;
break;
default:
return ‘application/octet-stream’;
break;
}
}
[/coolcode]
bbcode到html的转换:
[coolcode]
function bbcode2html($message) {
$searcharray1 = array(
‘[b]’,
‘[/b]’,
‘[i]’,
‘[/i]’,
‘[u]’,
‘[/u]’,
‘[/align]’,
‘[list]’,
‘[list=1]’,
‘[list=a]’,
‘[list=A]’,
‘[*]’,
‘[/list]’,
‘[fly]’,
‘[/fly]’,
‘[move]’,
‘[/move]’,
‘[/color]’,
‘[/size]’,
‘[/font]’
);

$replacearray1 = array(
‘,
‘,
‘,
‘,
‘,
‘,

‘,

    ‘,

      ‘,

        ‘,

          ‘,

        1. ‘,

‘,
‘,
‘,
‘,
‘,
‘‘,
‘‘,
‘‘
);
//$message = str_replace($searcharray, $replacearray, $message);

$searcharray2 = array(
“/\s*\[quote\][\n\r]*(.+?)[\n\r]*\[\/quote\]\s*/is”,
“/\s*\[code\](.+?)\[\/code\]\s*/ies”,
“/\s*\[glow=(\d+),(\w),(\d)\](.+?)\[\/glow\]\s*/ies”,
“/\[color=([^\[\<]+?)\]/i", "/\[size=([^\[\<]+?)\]/i", "/\[font=([^\[\<]+?)\]/i", "/\[align=([^\[\<]+?)\]/i", "/\s*\[free\][\n\r]*(.+?)[\n\r]*\[\/free\]\s*/is", "/\[url\]\s*(www.|https?:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|telnet:\/\/|rtsp:\/\/|mms:\/\/|callto:\/\/|ed2k:\/\/){1}([^\[\"']+?)\s*\[\/url\]/ie", "/\[url=www.([^\[\"']+?)\](.+?)\[\/url\]/is", "/\[url=(https?|ftp|gopher|news|telnet|rtsp|mms|callto|ed2k){1}:\/\/([^\[\"']+?)\](.+?)\[\/url\]/is", "/\[email\]\s*([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\s*\[\/email\]/i", "/\[email=([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\](.+?)\[\/email\]/is", "/\[swf\]\s*([^\[\<\r\n]+?)\s*\[\/swf\]/ies", "/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies", "/\[img=(\d{1,3})[x|\,](\d{1,3})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies" ); $replacearray2 = array( "

QUOTE:
\\1

“,
“codedisp(‘\\1’)”,

\\4

“,
“,
“,
“,

“,

FREE:
\\1

“,
“cuturl(‘\\1\\2’)”,
\\2“,
\\3“,
\\1@\\2“,
\\3“,
“bbcodeurl(‘\\1’, ‘ Flash: %s ‘)”,
“bbcodeurl(‘\\1’, ‘screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=\’Click here to open new window\\nCTRL+Mouse wheel to zoom in/out\’;}\” onmouseover=\”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=\’hand\’; this.alt=\’Click here to open new window\\nCTRL+Mouse wheel to zoom in/out\’;}\” onclick=\”if(!this.resized) {return true;} else {window.open(\’%s\’);}\” onmousewheel=\”return imgzoom(this);\”>’)”,
“bbcodeurl(‘\\3’, ‘‘)”
);
return preg_replace($searcharray2, $replacearray2, str_replace($searcharray1, $replacearray1, $message));
}
[/coolcode]

原创文章,转载请注明: 转载自www.webmaster.me

本文链接地址: 收集一些实用的小函数

标签:

相关推荐
更多

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*