匹配首尾空白字符的正则表达式:^s*|s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
$action = trim($_get['action']);
if($action == "sub")
{
$str = $_post['dir'];
//if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."a-za-z0-9_]+$/",$str)) //gb2312汉字字母数字下划线正则表达式
if(!preg_match("/^[x{4e00}-x{9fa5}a-za-z0-9_]+$/u",$str)) //utf-8汉字字母数字下划线正则表达式
{
echo "<font color=red>您输入的[".$str."]含有违法字符</font>";
}
else
{
echo "<font color=green>您输入的[".$str."]完全合法,通过!</font>";
}
}
?>
<form method="post" action="">
输入字符(数字,字母,汉字,下划线):
<input type="text" name="dir" value="">
<input type="submit" value="提交">
</form>
匹配网址url的正则表达式:[a-za-z]+://[^s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-za-z][a-za-z0-9_]{4,15}$
评注:表单验证时很实用
匹配国内电话号码:d{3}-d{8}|d{4}-d{7}
评注:匹配形式如 0511-4405222 或 021-87888822
匹配腾讯qq号:[1-9][0-9]{4,}
评注:腾讯qq号从10000开始
匹配中国邮政编码:[1-9]d{5}(?!d)
评注:中国邮政编码为6位数字
匹配身份证:d{15}|d{18}
评注:中国的身份证为15位或18位
匹配ip地址:d+.d+.d+.d+
评注:提取ip地址时有用
$str ="<a >fdafsa''&#x</a>";
echo htmlspecialchars( $str );
/*
& (和) 转成 &
" (双引号) 转成 "
< (小于) 转成 <
> (大于) 转成 >
此函式只转换上面的特殊字元,并不会全部转换成 html 所定的 ascii 转换
*/
//输出
html_entity_decode($str);
/*
于html_entity_decode在大多数情况下是与htmlspecialchars htmlentities配合使用的
*/
代码如下 | 复制代码 |
function mathweek( $date ) { $first_day_week = date( "n" , mktime ( 0 , 0 , 0 , 1 , 1 , date( 'y' , $date ) ) ); $first_week_days = 7 - $first_day_week + 1; $day_index = date( "z" , $date ) + 1 - $first_week_days ; $day_index = $day_index ? $day_index : 0 ; return ceil($day_index/7) + 1; } $date = strtotime('2010-08-12'); echo mathweek($date); ?> |
asp教程.net计算方法
代码如下 | 复制代码 |
system.globalization.gregoriancalendar gc = new system.globalization.gregoriancalendar(); int weekofyear = gc.getweekofyear(datetime.now, system.globalization.calendarweekrule.firstday, dayofweek.monday); |
在php中有修改文件名的函数,rename($o,$n),$o是旧文件名,$n要被修改成的新文件名哦。下面我们来看一款关于重名命文件名的实例
代码如下 | 复制代码 |
<?php session_start(); if($submit=="提交"){ if (file_exists( $old_name)){ if (rename($old_name,$new_name )) //把原文件重新命名 {echo "修改成功!";} }else{ print $old_name."文件不存在!<br>" ; } } ?> |
代码如下 | 复制代码 |
$email ='v1055494988@qq.com'; if (!eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$email)) { |