+ -
当前位置:首页 → 问答吧 → 1-5题本人愚解

1-5题本人愚解

时间:2008-10-15

来源:互联网

[php]
//  NO.1
/**
*  遍历一个文件夹下的文件及子文件夹中的文件   
*  array   function crawl_dirs(string $dir)   
*  $dir:路径
*  print_r(crawl_dirs(dirname(__FILE__))); // return array();
*  
*/
function crawl_dirs($dir)
{
    $array = array();
    $d = @dir($dir);
    if($d){
        while (false !== ($entry = $d->read())){
            if($entry!='.' && $entry!='..'){
                $key   = $entry;
                $entry = $dir.'/'.$entry;
                if(is_dir($entry)){
                    $subdirs = crawl_dirs($entry);
                    if ($subdirs)
                        $array[$key] = $subdirs;
                    else
                        $array[] = $entry;
                }else{
                    $array = array_merge($array, (array)$key);
                }
            }
        }
        $d->close();
    }
    return $array;
}


//  NO.2
/**
*  对日期进行加减操作
*  
*  string adddate ( int $sub [, string $date ][, int $type ] )
*  
*  $sub    : +增加 -减少.(天数)
*  $date   : 日期字符串或UNIX时间戳(取决于$type)[默认为time()]  
*  $type   : 1表示$date字符串日期 否则为时间戳[默认为字符串日期]
*
*  adddate(5,'2008-09-10');    //  return 2008-9-15;
*  adddate(4);                 //  return now+4;
*/
function adddate($sub,$date = false,$type = 1){
    if(!$date)
        $date = time();
    else
        $date   = $type ? strtotime ($date) : $date;
   
    $oneday = 60*60*24;
    $date   += $oneday * $sub;
    return  date("Y-m-d",$date);
}


//  NO.3
/**
*   check_mail('[email protected]');               // 5555555纯数字
*   check_mail('[email protected]');             // 用户名纯数字
*   check_mail('[email protected]');                // 不可为163邮箱
*   check_mail('[email protected]');             // OK
*   check_mail('[email protected]');                 // OK
*   check_mail('[email protected]');          // OK
*
*/
function check_mail($mail){
   
    // 不允许使用的
    $_host  = "(?!sina\.com(?!\.cn)|263\.com(?!\.cn)|163\.com(?!\.cn)|126\.com(?!\.cn)|qq\.com(?!\.cn))";
   
    // 后缀格式
    $host   = "([a-z0-9\_-]*)[\.][a-z0-9]{2,3}([\.][a-z]{2})?";   
   
    // 格式验证正则
    $base   = "/^([0-9a-z\_-]+)@(".$_host.$host.")$/i";
   
    // 数字正则
    $num    = "/^[\d]*$/i";
   
    $pt = -1;
   
    if (preg_match($base,$mail,$tmp)) {
        //print_r($tmp);
        if( preg_match($num,$tmp[1]) || preg_match($num,$tmp[3])){
            return $pt;
        }else{
            if(1){
                // 数据库查找是否重名 function dbcheck()
                $pt = 1;
            }   
        }
    }
   
    return $pt;
}

//  NO.4     SQL语句
$sql = "select topic.*
        from user,topic,rank
        where user.rankid = rank.id and user.id = topic.userid
              and  topic.belongid = 5";


//  NO.5    MySQL数据库中如何避免SQL注入(使用PHP编程方式实现。)
function inputfilter($input){
        $input = trim($input);
        $input = (! get_magic_quotes_gpc ()) ? addslashes ($input) : $input;
        return $input;
}

[/php]

作者: 125231896   发布时间: 2008-10-15

都回答了呀

作者: dohelp   发布时间: 2008-11-17