首页 > 编程技术 > php

php邮箱地址正则验证实例函数

发布时间:2016-11-25 15:11

 代码如下 复制代码

$email ='v1055494988@qq.com';
//echo preg_match('w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*',$email,$arr);

if (!eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$email)) {
  echo "您的 e-mail 通过初步检查";
}

 

在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>" ; }
}
?>

 

我们经常会碰到采集内容时,要补全地址是吧,下面我们来看看这款php把相对路径转换成绝对路径方法吧。

相对路径转化成绝对路径

 代码如下 复制代码

function relative_to_absolute($content, $feed_url) {
    preg_match('/(http|https教程|ftp):///', $feed_url, $protocol);
    $server_url = preg_replace("/(http|https|ftp|news):///", "", $feed_url);
    $server_url = preg_replace("//.*/", "", $server_url);

    if ($server_url == '') {
        return $content;
    }

    if (isset($protocol[0])) {
        $new_content = preg_replace('/href="//', 'href="'.$protocol[0].$server_url.'/', $content);
        $new_content = preg_replace('/src="//', 'src="'.$protocol[0].$server_url.'/', $new_content);
    } else {
        $new_content = $content;
    }
    return $new_content;
}

//取得所有链接

 代码如下 复制代码
function get_all_url($code){
        preg_match_all('/<as+href=["|']?([^>"' ]+)["|']?s*[^>]*>([^>]+)</a>/i',$code,$arr);
        return array('name'=>$arr[2],'url'=>$arr[1]);
本教程提供了三款获取上传文件与图片类型的方法,方法也是很简单的前二种,是先把类型定义好,再用in_array判断,最后一步是利用了fopen再读取前二个字节,判断。
 代码如下 复制代码

//获得图片的格式,包括jpg,png,gif
function get_type($img_name)//获取图像文件类型
{
 if (preg_match("/.(jpg|jpeg|gif|png|bmp)$/i", $img_name, $matches)){
  $type = strtolower($matches[1]);
 }else{
  $type = "string";
 }
 return $type;
}

//判断上传文件类型
$allowedextensions = array("txt","csv","htm","html","xml",  
    "css教程","doc","xls","rtf","ppt","pdf","swf","flv","avi",  
    "wmv","mov","jpg","jpeg","gif","png");   
    foreach ($_files as $file) {  
    if ($file['tmp_name'] > '') {  
      if (!in_array(end(explode(".",  
            strtolower($file['name']))),  
            $allowedextensions)) {  
       die($file['name'].' is an invalid file type!<br/>'.  
        '<a href="网页特效:history.go(-1);">'.  
        '&lt;&lt go back</a>');  
      }  
}  
}  

//另类的读取文件与图片类型

function checktitle($filename) {
$file     = fopen($filename, "rb");
$bin      = fread($file, 2); //只读2字节
fclose($file);
$strinfo  = @unpack("c2chars", $bin);
$typecode = intval($strinfo['chars1'].$strinfo['chars2']);
$filetype = "";
switch ($typecode)
{
 case 7790:
 $filetype = 'exe';
 break;
 case 7784:
 $filetype = 'midi';
 break;
 case 8297:
 $filetype = 'rar';
 break;
 case 255216:
 $filetype = 'jpg';
 break;
 case 7173:
 $filetype = 'gif';
 break;
 case 6677:
 $filetype = 'bmp';
 break;
 case 13780:
 $filetype = 'png';
 break;
 default:
 $filetype = 'unknown'.$typecode;
}
//fix
if ($strinfo['chars1']=='-1' && $strinfo['chars2']=='-40' ) {
return 'jpg';
}
if ($strinfo['chars1']=='-119' && $strinfo['chars2']=='80' ) {
return 'png';
}
return $filetype;
}

 

?>

定义和用法
header() 函数向客户端发送原始的 http 报头。

认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 php教程 4 以及更高的版本中,您可以使用输出缓存来解决此问题):

<html>

实例一

 代码如下 复制代码
<?php # script 2.7 - view_tasks.php
// connect to the database:
$dbc = @mysql教程i_connect ('localhost', 'username', 'password', 'test') or die ('<p>could not connect to the database!</p></body></html>');
// get the latest dates as timestamps教程:
$q = 'select unix_timestamp(max(date_added)), unix_timestamp(max(date_completed)) from tasks';
$r = mysqli_query($dbc, $q);
list($max_a, $max_c) = mysqli_fetch_array($r, mysqli_num);
// determine the greater timestamp:
$max = ($max_a > $max_c) ? $max_a : $max_c;
// create a cache interval in seconds:
$interval = 60 * 60 * 6; // 6 hours
// send the header:
header ("last-modified: " . gmdate ('r', $max));
header ("expires: " . gmdate ("r", ($max + $interval)));
header ("cache-control: max-age=$interval");
?>

实例二

 代码如下 复制代码

<?php
// 结果出错
// 在调用 header() 之前已存在输出
header('location: http://www.111cn.net/');
?>语法
header(string,replace,http_response_code)

提示用户保存一个生成的 pdf 文件(content-disposition 报头用于提供一个推荐的文件名,并强制浏览器显示保存对话框):

 代码如下 复制代码

<?php
header("content-type:application/pdf");

// 文件将被称为 downloaded.pdf
header("content-disposition:attachment;filename='downloaded.pdf'");

// pdf 源在 original.pdf 中
readfile("original.pdf");
?>

<html>
<body>

标签:[!--infotagslink--]

您可能感兴趣的文章: