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";
?>
----
文件下载头部输出如何设定
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'
用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");
正则匹配中文
ereg("^[".chr(0xa1)."-".chr(0xff)."]+$", $str);
批量替换文本里面的超级链接
<?php
function urlParse($str = ''){
if ('' == $str) return $str;
$types = array("http", "ftp", "https");
$replace = <<<EOPHP
'<a href="'.htmlentities('/1').htmlentities('/2').'">'.htmlentities('/1').htmlentities('/2').'</a>'
EOPHP;
$ret = $str;
while(list(,$type) = each($types)){
$ret = preg_replace("|($type://)([^/s]*)|ie ", $replace, $ret);
}
return $ret;
}
?>