标签:
1、PHP加密解密
function encryptDecrypt($key, $string, $decrypt){
if($decrypt){
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");
return $decrypted;
}else{
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
return $encrypted;
}
}
//加密:
echo encryptDecrypt(‘password‘, ‘Helloweba欢迎您‘,0);
//解密:
echo encryptDecrypt(‘password‘, ‘z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=‘,1);
2、PHP生成随机字符串
function generateRandomString($length = 10) {
$characters = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘;
$randomString = ‘‘;
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
echo generateRandomString(20);
3、PHP获取文件扩展名(后缀)
function getExtension($filename){
$myext = substr($filename, strrpos($filename, ‘.‘));
return str_replace(‘.‘,‘‘,$myext);
}
$filename = ‘我的文档.doc‘;
echo getExtension($filename);
4、PHP获取文件大小并格式化
function formatSize($size) {
$sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
if ($size == 0) {
return(‘n/a‘);
} else {
return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]);
}
}
$thefile = filesize(‘test_file.mp3‘);
echo formatSize($thefile);
5、PHP替换标签字符
function stringParser($string,$replacer){
$result = str_replace(array_keys($replacer), array_values($replacer),$string);
return $result;
}
$string = ‘The {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself‘;
$replace_array = array(‘{b}‘ => ‘<b>‘,‘{/b}‘ => ‘</b>‘,‘{br}‘ => ‘<br />‘);
echo stringParser($string,$replace_array);
6、PHP获取当前页面URL
function curPageURL() {
$pageURL = ‘http‘;
if (!empty($_SERVER[‘HTTPS‘])) {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
echo curPageURL();
7、PHP强制下载文件
function download($filename){
if ((isset($filename))&&(file_exists($filename))){
header("Content-length: ".filesize($filename));
header(‘Content-Type: application/octet-stream‘);
header(‘Content-Disposition: attachment; filename="‘ . $filename . ‘"‘);
readfile("$filename");
} else {
echo "Looks like file does not exist!";
}
}
download(‘/down/test_45f73e852.zip‘);
8、PHP截取字符串长度
/*
Utf-8、gb2312都支持的汉字截取函数
cut_str(字符串, 截取长度, 开始长度, 编码);
编码默认为 utf-8
开始长度默认为 0
*/
function cutStr($string, $sublen, $start = 0, $code = ‘UTF-8‘){
if($code == ‘UTF-8‘){
$pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join(‘‘, array_slice($t_string[0], $start, $sublen))."...";
return join(‘‘, array_slice($t_string[0], $start, $sublen));
}else{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = ‘‘;
for($i=0; $i<$strlen; $i++){
if($i>=$start && $i<($start+$sublen)){
if(ord(substr($string, $i, 1))>129){
$tmpstr.= substr($string, $i, 2);
}else{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
$str = "jQuery插件实现的加载图片和页面效果";
echo cutStr($str,16);
9、
PHP常用代码段:
标签:
原文地址:http://www.cnblogs.com/mracale/p/5671718.html