PHP小技巧摘录

目录名称合法性检测函式
//目录名称合法性检测
function isen($str){
$ret=”";
for($i=0;$i $p=ord(substr($str,$i,1));
if(($p<48 & $p!=45 & $p!=46) || ($p>57 & $p<65) || ($p>90 & $p<97 & $p!=95) || $p>122){
nerror(“不符合规范!”);
}
$ret.=chr($p);
}
return $ret;
}

PHP如何判断ip地址合法性
if(!strcmp(long2ip(sprintf(“%u”,ip2long($ip))),$ip)) echo “is ipn”;
—-
email的正则判断
eregi(“^[_.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z_-]+.)+[a-zA-Z]$”, $email);

检测ip地址和mask是否合法的例子


$ip = ’192.168.0.84′;
$mask = ’255.255.255.0′;
$network = ’192.168.0′;
$ip = ip2long($ip);
$mask = ip2long($mask);
$network = ip2long($network);
if( ($ip & $mask) == $network) echo “valid ip and maskn”;
?>
—-
PHP文件下载头部输出如何设定
header(“Content-type: application/x-download”);
header(“Content-Disposition: attachment; filename=$file_download_name;”);
header(“Accept-Ranges: bytes”);
header(“Content-Length: $download_size”);
echo ‘xxx’
PHP用header输出ftp下载方式,并且支持断点续传
一个例子:
header(‘Pragma: public’);
header(‘Cache-Control: private’);
header(‘Cache-Control: no-cache, must-revalidate’);
header(‘Accept-Ranges: bytes’);
header(‘Connection: close’);
header(“Content-Type: audio/mpeg”);
header(“Location:ftp://download:1bk3l4s3k9s2@232.2.22.22/2222/web技术开发知识库/cn_web.rmvb”);
PHP正则匹配中文
ereg(“^[".chr(0xa1)."-".chr(0xff)."]+$”, $str);
批量替换文本里面的超级链接
function urlParse($str = ”){
if (” == $str) return $str;
$types = array(“http”, “ftp”, “https”);
$replace = <<
”.htmlentities(’1′).htmlentities(’2′).”
EOPHP;
$ret = $str;
while(list(,$type) = each($types)){
$ret = preg_replace(“|($type://)([^s]*)|ie “, $replace, $ret);
}
return $ret;
}

 

 

 

posted @ 2012-10-08 15:52  原来我不帥  阅读(137)  评论(0编辑  收藏  举报