php preg_match_all 与preg_match 区别与实例

 更新时间:2016年11月25日 15:54  点击:1887

/*

 代码如下 复制代码
int preg_match_all ( string $pattern , string $subject , array &$matches [, int $flags [, int $offset ]] );

搜索所有匹配正则表达式的模式并提出给予他们在比赛中受的标志指定的顺序。第一场比赛后发现,随后的搜查是继续从最后一场比赛结束。

实例

 代码如下 复制代码
preg_match_all("|<[^>]+>(.*)</[^>]+>|u",
    "<b>example: </b><div align=left>this is a test</div>",
    $out, preg_pattern_order);
echo $out[0][0] . ", " . $out[0][1] . " ";
echo $out[1][0] . ", " . $out[1][1] . " ";

出输

 代码如下 复制代码

<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test


preg_match_all("|<[^>]+>(.*)</[^>]+>|u",
    "<b>example: </b><div align="left">this is a test</div>",
    $out, preg_set_order);
echo $out[0][0] . ", " . $out[0][1] . " ";
echo $out[1][0] . ", " . $out[1][1] . " ";

 

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )
搜索主题的经常表达的方式给予配合


$subject = "abcdefwww.111cn.net";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, preg_offset_capture);
print_r($matches);


array
(
    [0] => array
        (
            [0] => def
            [1] => 0
        )

)


*/

 代码如下 复制代码
function ihtmlspecialchars($string)
{
 if(is_array($string))
 {
  foreach($string as $key => $val)
  {
   $string[$key] = ihtmlspecialchars($val);
  }
 } else
 {
  $string = preg_replace('/&amp;((#(d{3,5}|x[a-fa-f0-9]{4})|[a-za-z][a-z0-9]{2,5});)/', '&\1',
  str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
 }
 return $string;
}

//实例

 代码如下 复制代码

$str = '<a href="www.111cn.net">dfdfd</a>';
echo ihtmlspecialchars( $str );
echo '<br >';

//echo &lt;a href=&quot;www.111cn.net&quot;&gt;dfdfd&lt;/a&gt; 这样就可防止一些不安全因素了。

echo htmlspecialchars ($str );

//print &lt;a href=&quot;www.111cn.net&quot;&gt;dfdfd&lt;/a&gt;

/*
两个结果完全相同,所以个人觉得自php自带的函数高效于用户自定义函数
*/
?>

//加上

 代码如下 复制代码
header('p3p: cp="cura adma deva ps教程ao psdo our bus uni pur int dem sta pre com nav otc noi dsp cor"');

//方法二针对二级域名

 代码如下 复制代码
setcookie('loaddomain','http://'.$url,time()+3600*24,'/','.111cn.net');

//这样的话,所有111cn.net的二级域名都同时登陆了。

date_default_timezone_set('prc');
/**
* 求取从某日起经过一定天数后的日期,
* 排除周六周日和节假日
* @param $start       开始日期
* @param $offset      经过天数
* @param $exception 例外的节假日
* @param $allow       允许的日期(预留参数)
* @return
*  examples:输入(2010-06-25,5,''),得到2010-07-02
*/
function getendday( $start='now', $offset=0, $exception='', $allow='' ){
    //先计算不排除周六周日及节假日的结果
    $starttime = strtotime($start);
    $endtime = $starttime + $offset * 24 * 3600;
    $end = date('y-m-d', $endtime);
    //然后计算周六周日引起的偏移
    $weekday = date('n', $starttime);//得到星期值:1-7
    $remain = $offset % 7;
    $newoffset = 2 * ($offset - $remain) / 7;//每一周需重新计算两天
    if( $remain > 0 ){//周余凑整
        $tmp = $weekday + $remain;
        if( $tmp >= 7 ){
            $newoffset += 2;
        }else if( $tmp == 6 ){
            $newoffset += 1;
        }
        //考虑当前为周六周日的情况
        if( $weekday == 6 ){
            $newoffset -= 1;
        }else if( $weekday == 7 ){
            $newoffset -= 2;
        }
    }
    //再计算节假日引起的偏移
    if( is_array($exception) ){//多个节假日
        foreach ($exception as $day){
            $tmp_time = strtotime($day);
            if( $tmp_time>$starttime && $tmp_time<=$endtime ){//在范围(a,b]内
                $weekday_t = date('n', $tmp_time);
                if($weekday_t <= 5){//防止节假日与周末重复
                    $newoffset += 1;
                }
            }
        }
    }else{//单个节假日
        if( $exception!='' ){
            $tmp_time = strtotime($exception);
            if( $tmp_time>$starttime && $tmp_time<=$endtime ){
                $weekday_t = date('n', $tmp_time);
                if($weekday_t <= 5){
                    $newoffset += 1;
                }
            }
        }
       
    }
    //根据偏移天数,递归做等价运算111cn.net
    if($newoffset > 0){
        #echo "[{$start} -> {$offset}] = [{$end} -> {$newoffset}]"."<br /> ";
        return getendday($end,$newoffset,$exception,$allow);
    }else{
        return $end;
    }
}
/**
* 暴力循环方法
*/
function getendday2( $start='now', $offset=0, $exception='', $allow='' ){
    $starttime = strtotime($start);
    $tmptime = $starttime + 24*3600;
   
    while( $offset > 0 ){
        $weekday = date('n', $tmptime);
        $tmpday = date('y-m-d', $tmptime);
        $bfd = false;//是否节假日
        if(is_array($exception)){
            $bfd = in_array($tmpday,$exception);
        }else{
            $bfd = ($exception==$tmpday);
        }
        if( $weekday<=5 && !$bfd){//不是周末和节假日
            $offset--;
            #echo "tmpday={$tmpday}"."<br />";
        }
        $tmptime += 24*3600;
    }
   
    return $tmpday;
}
$exception = array(
    '2010-01-01','2010-01-02','2010-01-03',
    '2010-04-03','2010-04-04','2010-04-05',
    '2010-05-01','2010-05-02','2010-05-03',
    '2010-06-14','2010-06-15','2010-06-16',
    '2010-09-22','2010-09-23','2010-09-24',
    '2010-10-01','2010-10-02','2010-10-03','2010-10-04',
    '2010-10-05','2010-10-06','2010-10-07',
   
);
//echo getendday('2010-08-27',3,'');
//echo getendday('2010-06-25',15,'2010-07-07');
$t1 = microtime();
echo getendday('2010-05-12',66,$exception)."<br />";
$t2 = microtime();echo "use ".($t2-$t1)." s <br />";
echo getendday2('2010-05-12',66,$exception)."<br />";
$t3 = microtime();echo "use ".($t3-$t2)." s <br />";

本程序是根据用户上传图片后再把上传的图片按比例生成缩略图
 代码如下 复制代码

<?
$w?$resizewidth=$w:$resizewidth=400;// 生成图片的宽度
$h?$resizeheight=$h:$resizeheight=400;// 生成图片的高度
function resizeimage($im,$maxwidth,$maxheight,$name){
    $width = imagesx($im);
    $height = imagesy($im);
    if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
        if($maxwidth && $width > $maxwidth){
            $widthratio = $maxwidth/$width;
            $resizewidth=true;
        }
        if($maxheight && $height > $maxheight){
            $heightratio = $maxheight/$height;
            $resizeheight=true;
        }
        if($resizewidth && $resizeheight){
            if($widthratio < $heightratio){
                $ratio = $widthratio;
            }else{
                $ratio = $heightratio;
            }
        }elseif($resizewidth){
            $ratio = $widthratio;
        }elseif($resizeheight){
            $ratio = $heightratio;
        }
        $newwidth = $width * $ratio;
        $newheight = $height * $ratio;
        if(function_exists("imagecopyresampled")){
              $newim = imagecreatetruecolor($newwidth, $newheight);
              imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }else{
            $newim = imagecreate($newwidth, $newheight);
              imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }
        imagejpeg ($newim,$name);
        imagedestroy ($newim);
    }else{
        imagejpeg ($im,$name);
    }
}

if($_files['uploadfile']['size']){
    if($_files['uploadfile']['type'] == "image/pjpeg"){
        $im = imagecreatefromjpeg($_files['uploadfile']['tmp_name']);
    }elseif($_files['uploadfile']['type'] == "image/x-png"){
        $im = imagecreatefrompng($_files['uploadfile']['tmp_name']);
    }elseif($_files['uploadfile']['type'] == "image/gif"){
        $im = imagecreatefromgif($_files['uploadfile']['tmp_name']);
    }
    if($im){
        if(file_exists('bbs.jpg')){
            unlink('bbs.jpg');
        }
        resizeimage($im,$resizewidth,$resizeheight,'bbs.jpg');
        imagedestroy ($im);
  
    }
}
//$uploadfile="www.111cn.net.jpg";
?>

[!--infotagslink--]

相关文章

  • mysql_connect与mysql_pconnect的区别详解

    在mysql中我们会看到有两种常用的数据库连接模式,一种是长久连接,另一各是页面访问完之后就断了连接,下面我来分别介绍mysql_connect与mysql_pconnect的区别,有需要了解...2016-11-25
  • C#中out与ref的区别实例解析

    这篇文章主要介绍了C#中out与ref的区别实例解析,对C#初学者有不错的学习借鉴价值,需要的朋友可以参考下...2020-06-25
  • PHP中func_get_args(),func_get_arg(),func_num_args()的区别

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04
  • 谈谈Jquery中的children find 的区别有哪些

    精华:find方法能找子孙,children方法只能找儿子一、Jquery中children 语法.children(selector) 说明expr是表达式,可选参数,所有选择器中的表达式都可以用在这,比如按标签名"div",按类名".class",按序号":first"等等,如果表...2015-10-21
  • PS中像素大小、文档大小的区别

    在PS中像素大小、文档大小有什么区别呢,这个估计很多初学者不清楚,下面我来给大家讲解一下,希望对你有帮助。 1、像素大小 通常用于显示屏显示的图片大小的调整。菜...2016-09-14
  • input框中的name和id的区别

    这篇文章主要介绍了input框中的name和id的区别介绍,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-11-22
  • yii添删改查实例

    一、数据访问对象 (DAO)YiiDAO 基于 PHP Data Objects (PDO) 构建。它是一个为众多流行的DBMS提供统一数据访问的扩展,这些 DBMS 包括MySQL, PostgreSQL 等等。因此,要使用 Yii DAO,PDO 扩展和特定的 PDO 数据库驱动(例如...2015-11-24
  • 快速理解MySQL中主键与外键的实例教程

    主键与外键的关系,通俗点儿讲,我现在有一个论坛,有两张表,一张是主贴 thread,一张是回帖 reply先说说主键,主键是表里面唯一识别记录的字段,一般是帖子id,体现在访问的时候,例如是 thread.php&#63;id=1 表示我要访问的是帖子...2015-11-24
  • C#中sleep和wait的区别分析

    这篇文章主要介绍了C#中sleep和wait的区别分析,有助于深入理解C#中线程的原理与使用技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • uniapp和vue的区别详解

    这篇文章主要介绍了uniapp和vue的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-10-19
  • c# 接口使用实例

    这篇文章主要介绍了c#接口使用的实例,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-17
  • list与push的区别

    //函数list while(list($id,$username,$password,$add_date,$mdn,$mobile,$channel,$last_date,$area,$nickname) = mysql_fetch_array($rs)){ ...2016-11-25
  • php switch 与 if else 区别

    在php中switch是选择,if else也有同理,但是它们肯定是有区别的,那么我们来看看它们两者的区别在哪里呢,下面先看switch case语句吧。 switch($id){ case 1: ...2016-11-25
  • C#中的IEnumerable简介及简单实现实例

    这篇文章主要介绍了C#中的IEnumerable简介及简单实现实例,本文讲解了IEnumerable一些知识并给出了一个简单的实现,需要的朋友可以参考下...2020-06-25
  • PHP中file_exists与is_file,is_dir的区别

    在php中file_exists与is_file,is_dir都可以用来检测目录或文件是否存在了,那么它们三者的具体区别在哪里呢,下面我们一起来看看吧。 很显然file_exists是受了asp...2016-11-25
  • 浅谈C++中字符串输入get与getline的区别

    这篇文章主要介绍了C++中字符串输入get与getline的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • php mysql localhost,127.0.0.1和ip区别

    一家之言:localhost与127.0.0.1的区别 localhost与127.0.0.1的区别是什么?相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少一次解析。看来这个入门问题还有人不清楚,其实这两者是有区别的。no1:localhos...2014-05-31
  • C#中类与接口的区别个人总结

    这篇文章主要介绍了C#中类与接口的区别个人总结,本文讲解了类与接口的区别、接口的用处主要体现在下面几个方面、一些接口的疑问等内容,需要的朋友可以参考下...2020-06-25
  • vue实例的选项总结

    这篇文章主要介绍了Vue实例的选项有哪些,文中讲解非常细致,代码帮助大家更好的学习,感兴趣的朋友可以了解下...2020-06-10
  • 详解CSS3中nth-child与nth-of-type的区别

    这篇文章详细解析了CSS3中nth-child与nth-of-type的区别,有兴趣的同学可以参考一下 CSS3中nth-child与nth-of-type的区别其实很简单::nth-of-type为什么要叫:nth-of...2017-01-22