在php中地址验证写法各种各样的,下面我来总结几种常用的email地址验证实例,最简单的是直接使用正则表达式preg_match(\"/^([a-z0-9\\+_\\-]+)(\\.[a-z0-9\\+_\\-]+)*@([a-z0-9\\-]+\\.)+[a-z]{2,6}$/ix来验证了。
CodeIgniter框架邮件地址验证
代码如下 |
复制代码 |
/**
* Valid Email
*
* @access public
* @param string
* @return bool
*/
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9+_-]+)(.[a-z0-9+_-]+)*@([a-z0-9-]+.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
|
PHPCMS邮件正则验证
代码如下 |
复制代码 |
/**
* 判断email格式是否正确
* @param $email
*/
function is_email($email) {
return strlen($email) > 6 && preg_match("/^[w-.]+@[w-.]+(.w+)+$/", $email);
}
|
WordPress邮件地址验证函数
代码如下 |
复制代码 |
/**
* Verifies that an email is valid.
*
* Does not grok i18n domains. Not RFC compliant.
*
* @since 0.71
*
* @param string $email Email address to verify.
* @param boolean $deprecated Deprecated.
* @return string|bool Either false or the valid email address.
*/
function is_email( $email, $deprecated = false ) {
if ( ! empty( $deprecated ) )
_deprecated_argument( __FUNCTION__, '3.0' );
// Test for the minimum length the email can be
if ( strlen( $email ) < 3 ) {
return apply_filters( 'is_email', false, $email, 'email_too_short' );
}
// Test for an @ character after the first position
if ( strpos( $email, '@', 1 ) === false ) {
return apply_filters( 'is_email', false, $email, 'email_no_at' );
}
// Split out the local and domain parts
list( $local, $domain ) = explode( '@', $email, 2 );
// LOCAL PART
// Test for invalid characters
if ( !preg_match( '/^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+$/', $local ) ) {
return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
}
// DOMAIN PART
// Test for sequences of periods
if ( preg_match( '/.{2,}/', $domain ) ) {
return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
}
// Test for leading and trailing periods and whitespace
if ( trim( $domain, " tnrx0B." ) !== $domain ) {
return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
}
// Split the domain into subs
$subs = explode( '.', $domain );
// Assume the domain will have at least two subs
if ( 2 > count( $subs ) ) {
return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
}
// Loop through each sub
foreach ( $subs as $sub ) {
// Test for leading and trailing hyphens and whitespace
if ( trim( $sub, " tnrx0B-" ) !== $sub ) {
return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
}
// Test for invalid characters
if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
}
}
// Congratulations your email made it!
return apply_filters( 'is_email', $email, $email, null );
}
|
例 自己写的
代码如下 |
复制代码 |
$email = "tanklo_--vehy@yahoo.com.cn";
function check_email($email) {
$pattern_test = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
return preg_match($pattern_test,$email);
}
echo check_email($email); |
本文章来给大家介绍关于php进行压缩 ob_gzhandler函数有3种方法,有需要了解ob_gzhandler函数用法的朋友不防进入参考。
使用ob_gzhandler函数有3种方法让它对php进行压缩:
1、在php.ini中设置output_handler = ob_gzhandler
2、在.htaccess中加入php_value output_handler ob_gzhandler
3、在php文件头加上ob_start(‘ob_gzhandler’);
第一种我们是没法办到,第二种方法不错,可以一劳永逸,一、二种方法效果都一样,就是对所有php后缀文件进行压缩。我这里主要介绍一下第三种方法,
就是试用CSS压缩法 例如对模板下的style.css进行压缩
1、把模板目录下的style.css复制一份出来,命名为style.css.php
接着在style.css.php顶部加入这句:
代码如下 |
复制代码 |
<?php if(extension_loaded('zlib')) {ob_start('ob_gzhandler');} header("Content-type: text/css"); ?> |
在最后加上
代码如下 |
复制代码 |
<?php if(extension_loaded('zlib')) {ob_end_flush();} ?> |
2、按照下面的方式修改header.php中的css连接
原来的:
代码如下 |
复制代码 |
<link rel="stylesheet" type="text/css" media="screen" href="/style.css"/>
|
修改后的:
代码如下 |
复制代码 |
<link rel="stylesheet" type="text/css" media="screen" href="/style.css.php"/>
|
搞定,现在你的CSS就可以被gzip压缩了。
JS也可以使用同样的方法,只是在xx.js.php头部加入的应该是这样:
代码如下 |
复制代码 |
<?php if ( extension_loaded('zlib') ) {ob_start('ob_gzhandler');} header("Content-Type: text/java script"); ?> |
(复制的时候去掉?php前的空格)
仔细看看K2主题的中的JS,就是修改后缀为php后在顶部加入了ob_gzhandler函数。
本文章来给大家分享一款不错的树形结构的php类代码,各位朋友可参考。
代码如下 |
复制代码 |
<?php
/**
* Tanphp framework
*
*
* @category Tanphp
* @package Data_structure
* @copyright Copyright (c) 2012 谭博 tanbo.name
* @version $Id: Tree.php 25024 2012-11-26 22:22:22 tanbo $
*/
/**
* 树形结构数据存取类
*
* 用于对树形结构数据进行快速的存取
*
* @param array $arr 参数必须为标准的二维数组,包含索引字段(id)与表示树形结构的字段(path),如example中所示
*
* @example <code>
* $arr = array(
* array( 'id' => 1, 'name' => 'php', 'path' => '1' ),
* array( 'id' => 3, 'name' => 'php1', 'path' => '1-3' ),
* array( 'id' => 2, 'name' => 'mysql', 'path' => '2' ),
* array( 'id' => 6, 'name' => 'mysql1', 'path' => '2-6' ),
* array( 'id' => 7, 'name' => 'mysql2', 'path' => '2-7' ),
* array( 'id' => 5, 'name' => 'php11', 'path' => '1-3-5' ),
* array( 'id' => 4, 'name' => 'php2', 'path' => '1-4' ),
* );
* $cate = new Tree($arr);
*
* $data = $cate->getChild(2);
*
* print_r($data->toArray());
* </code>
*
*/
class Tree
{
public $_info; //节点信息
public $_child = array(); //子节点
private $_parent; //父节点
private $_data; //当前操作的临时数据
private static $_indexs = array(); //所有节点的索引
private static $_index_key = 'id'; //索引键
private static $_tree_key = 'path'; //树形结构表达键
private static $_tree_delimiter = '-'; //属性结构表达分割符
/**
* 构造函数
*
* @param array $arr
* @param boole $force_sort 如果为真,将会强制对$arr 进行排序
* @return void
*/
public function __construct(array $arr = array(), $force_sort=true)
{
if ($force_sort === true) {
$arr=$this->_array_sort($arr, self::$_tree_key);
}
if (!empty($arr)) {
$this->_init($arr);
}
}
/**
* 初始存储树形数据
*
* @param array $arr
* @return void
*/
private function _init(array $arr)
{
foreach ($arr as $item) {
$path = $item[self::$_tree_key];
$paths = explode(self::$_tree_delimiter, $path);
$count_paths = count($paths);
$parent_id = isset($paths[$count_paths-2]) ? $paths[$count_paths-2] : NULL;
if ( $count_paths>1 //如果有父级
&& array_key_exists($parent_id, self::$_indexs) //父级已经被存入索引
&& self::$_indexs[$parent_id] instanceof Tree //父级为Tree对象
) {
self::$_indexs[$parent_id]->addChild($item);
} elseif ($count_paths == 1) {
$this->addChild($item);
} else {
throw new Exception("path数据错误".var_export($item, true));
}
}
//print_r(self::$_indexs);
}
/**
* 添加子节点
*
* @param array $item
* @return void
*/
public function addChild(array $item, $parent = NULL)
{
$child = new Tree();
$child->_info = $item;
$child->_parent = $parent == NULL ? $this : $parent;
$child->_parent->_child[] = $child;
$this->_addIndex($item, $child->_getSelf());
}
/**
* 添加节点到索引
*
* @param array $item
* @param mix $value
* @return void
*/
private function _addIndex(array $item, $value)
{
if (array_key_exists(self::$_index_key, $item) && is_int($item[self::$_index_key])) {
self::$_indexs[$item[self::$_index_key]] = $value;
} else {
throw new Exception("id字段不存在或者不为字符串");
}
}
/**
* 获取对自己的引用
*
* @return Tree object quote
*/
private function _getSelf()
{
return $this;
}
/**
* 获取指定id的节点的子节点
*
* @param int $id
* @return Tree object
*/
public function getChild($id)
{
$data = self::$_indexs[$id]->_child;
$this->_data = $data;
return $this;
}
/**
* 获取指定id的节点的父节点
*
* @param int $id
* @return Tree object
*/
public function getParent($id)
{
$data = self::$_indexs[$id]->_parent;
$this->_data = $data;
return $this;
}
/**
* 获取指定id的节点的同级节点
*
* @param int $id
* @return Tree object
*/
public function getBrother($id)
{
$data = self::$_indexs[$id]->_parent->_child;
$this->_data = $data;
return $this;
}
/**
* 将Tree对象转化为数组
*
* @param object $object
* @return array
*/
public function toArray($obj = NULL)
{
$obj = ($obj === NULL) ? $this->_data : $obj;
$arr = array();
$_arr = is_object($obj) ? $this->_getBaseInfo($obj) : $obj;
if (is_array($_arr)) {
foreach ($_arr as $key => $val){
$val = (is_array($val) || is_object($val)) ? $this->toArray($val) : $val;
$arr[$key] = $val;
}
} else {
throw new Exception("_arr不是数组");
}
return $arr;
}
/**
* 过滤_parent等字段,以免造成无限循环
*
* @param object $obj
* @return void
*/
private function _getBaseInfo($obj)
{
$vars = get_object_vars($obj);
$baseInfo['_info'] = $vars['_info'];
$baseInfo['_child'] = $vars['_child'];
return $baseInfo;
}
/**
* 二维数组排序
*
* 根据指定的键名对二维数组进行升序或者降序排列
*
* @param array $arr 二维数组
* @param string $keys
* @param string $type 必须为 asc或desc
* @throws 当参数非法时抛出异常
* @return 返回排序好的数组
*/
private function _array_sort(array $arr, $keys, $type = 'asc') {
if (!is_string($keys)) {
throw new Exception("非法参数keys:参数keys的类型必须为字符串");
}
$keysvalue = $new_array = array();
foreach ($arr as $k=>$v) {
if (!is_array($v) || !isset($v[$keys])) {
throw new Exception("参数arr不是二维数组或arr子元素中不存在键'{$keys}'");
}
$keysvalue[$k] = $v[$keys];
}
switch ($type) {
case 'asc':
asort($keysvalue);
break;
case 'desc':
arsort($keysvalue);
break;
default:
throw new Exception("非法参数type :参数type的值必须为 'asc' 或 'desc'");
}
reset($keysvalue);
foreach ($keysvalue as $k=>$v) {
$new_array[$k] = $arr[$k];
}
return $new_array;
}
}
?>
|
在php开发中我们经常会面要提供预定义判断变量或常量或函数是不是有了,下面我来介绍一些常用的判断常量、变量和函数是否存在应用实例。
常量检测使用defined,定义常量则是define。注意待检测的常量需要使用引号(单双均可),如:
代码如下 |
复制代码 |
if (defined('CONST_NAME')) {
//do something
}
|
变量检测则是使用isset,注意变量未声明或声明时赋值为NULL,isset均返回FALSE,如:
代码如下 |
复制代码 |
if (isset($var_name)) {
//do something
}
函数检测用function_exists,注意待检测的函数名也需要使用引号,如:
if (function_exists('fun_name')) {
fun_name();
}
|
先不说多了我们看一个实例
代码如下 |
复制代码 |
<?php
/* 判断常量是否存在*/
if (defined('MYCONSTANT')) {
echo MYCONSTANT;
}
//判断变量是否存在
if (isset($myvar)) {
echo "存在变量$myvar.";
}
//判断函数是否存在
if (function_exists('imap_open')) {
echo "存在函数imag_openn";
} else {
echo "函数imag_open不存在n";
}
?>
|
function_exists判断函数是否存在
代码如下 |
复制代码 |
<?php
if (function_exists('test_func')) {
echo "函数test_func存在";
} else {
echo "函数test_func不存在";
}
?>
|
filter_has_var函数
filter_has_var() 函数检查是否存在指定输入类型的变量。
若成功,则返回 true,否则返回 false。
代码如下 |
复制代码 |
<?php
if(!filter_has_var(INPUT_GET, "name"))
{
echo("Input type does not exist");
}
else
{
echo("Input type exists");
}
?>
|
输出为. Input type exists
本文章来给各位同学详细介绍关于php 计算两个日期这间的间隔天数实例,各位同学可参考,我们一般是把日期用strtotime转换,然后再进行算,这样可以精确到时分秒去哦。
例1
直接把日期转换
代码如下 |
复制代码 |
function daysbetweendates($date1, $date2){
$date1 = strtotime($date1);
$date2 = strtotime($date2);
$days = ceil(abs($date1 - $date2)/86400);
return $days;
}
|
例2
代码如下 |
复制代码 |
<?php
functionmaketime($date)
{
list($year,$month,$day) = explode('-',$date);
returnmktime(0,0,0,$month,$day,$year);
}
$date1 = '2007-01-08';
$date2 = '2007-03-01';
$d = (maketime($date2) - maketime($date1)) / (3600*24);
echo'相差$d 天';
?>
|
例3
PHP实现两个日期间隔的年、月、周、日数的计算
代码如下 |
复制代码 |
<?php
function format($a,$b){
//检查两个日期大小,默认前小后大,如果前大后小则交换位置以保证前小后大
if(strtotime($a)>strtotime($b)) list($a,$b)=array($b,$a);
$start = strtotime($a);
$stop = strtotime($b);
$extend = ($stop-$start)/86400;
$result['extends'] = $extend;
if($extend<7){ //如果小于7天直接返回天数
$result['daily'] = $extend;
}elseif($extend<=31){ //小于28天则返回周数,由于闰年2月满足了
if($stop==strtotime($a.'+1 month')){
$result['monthly'] = 1;
}else{
$w = floor($extend/7);
$d = ($stop-strtotime($a.'+'.$w.' week'))/86400;
$result['weekly'] = $w;
$result['daily'] = $d;
}
}else{
$y= floor($extend/365);
if($y>=1){ //如果超过一年
$start = strtotime($a.'+'.$y.'year');
$a = date('Y-m-d',$start);
//判断是否真的已经有了一年了,如果没有的话就开减
if($start>$stop){
$a = date('Y-m-d',strtotime($a.'-1 month'));
$m =11;
$y--;
}
$extend = ($stop-strtotime($a))/86400;
}
if(isset($m)){
$w = floor($extend/7);
$d = $extend-$w*7;
}else{
$m = isset($m)?$m:round($extend/30);
$stop>=strtotime($a.'+'.$m.'month')?$m:$m--;
if($stop>=strtotime($a.'+'.$m.'month')){
$d=$w=($stop-strtotime($a.'+'.$m.'month'))/86400;
$w = floor($w/7);
$d = $d-$w*7;
}
}
$result['yearly'] = $y;
$result['monthly'] = $m;
$result['weekly'] = $w;
$result['daily'] = isset($d)?$d:null;
}
return array_filter($result);
}
print_r(format('2012-10-1','2012-12-15'));
?> |