PHP函数正则表达式函数

  1. preg_grep   进行正则表达式匹配,返回与模式匹配的数组单元
    1 <?php
    2 $array=array("7人"=>"ASP部门","5人"=>"VB部门","6人"=>"PHP部门","8人"=>"Java 部门");
    3 $result1=preg_grep("/P+/",$array);
    4 print_r($result1);     //Array ( [7人] => ASP部门 [6人] => PHP部门 )
    5 $result2=preg_grep("/P+/",$array,PREG_GREP_INVERT);
    6 print_r($result2);     //Array ( [5人] => VB部门 [8人] => Java 部门 )
    7 ?>
  2. preg_match_all   进行全局正则表达式匹配,返回整个模式匹配的次数
    <?php
    $str="分类:<strong>PHP书籍</strong><br /> 名称:<strong>PHP 函数大全</strong>";
    preg_match_all("/<strong>(.*)<\/strong>/U",$str,$out);
    print_r($out);
    echo "<hr>";
    preg_match_all("/<strong>(.*)<\/strong>/U",$str,$out,PREG_SET_ORDER);
    print_r($out);
    echo "<hr>";
    preg_match_all("/<strong>(.*)<\/strong>/U",$str,$out,PREG_OFFSET_CAPTURE);
    print_r($out);
    ?>
    //Array ( [0] => Array ( [0] => PHP书籍 [1] => PHP 函数大全 ) [1] => Array ( [0] => PHP书籍 [1] => PHP 函数大全 ) ) 
    //--------------------------------------------------------------------------------
    //Array ( [0] => Array ( [0] => PHP书籍 [1] => PHP书籍 ) [1] => Array ( [0] => PHP 函数大全 [1] => PHP 函数大全 ) ) 
    //--------------------------------------------------------------------------------
    //Array ( [0] => Array ( [0] => Array ( [0] => PHP书籍 [1] => 5 ) [1] => Array ( [0] => PHP 函数大全 [1] => 41 ) ) [1] => Array ( [0] => Array ( [0] => PHP书籍 [1] => 13 ) [1] => Array ( [0] => PHP 函数大全 [1] => 49 ) ) ) 
  3. preg_match   进行正则表达式匹配,返回匹配的次数
     1 <?php   //E-mail 地址验证
     2 if(isset($_POST['email'])){
     3     if(preg_match("/(.+)@(\w+)\.([a-zA-Z]{2,})/", $_POST['email'])){
     4         echo "email地址合法";
     5     }
     6     else{
     7         echo "email地址不合法";
     8     }
     9 }
    10 ?>
  4. preg_quote   转义正则表达式字符
    1 <?php    //应用preg_quote()函数来过滤字符串的正则表达式语法字符
    2 $string='正则表达式的特殊字符包括: \ + * ? [ ^ ] $ ( ) { } = ! < > | : . ';
    3 echo preg_quote($string);    //正则表达式的特殊字符包括\: \\ \+ \* \? \[ \^ \] \$ \( \) \{ \} \= \! \< \> \| \: \.
    4 ?>
  5. preg_replace_callback   用回调函数执行正则表达式所匹配的搜索和替换
     1 <?php    //应用preg_replace_callback()函数来过滤字符串的正则表达式语法字符
     2 function strrep($str)
     3 {
     4     $arr=array("php"=>"PHP 函数大全","明日"=>"明日科技","邮电"=>"人民邮电出版社");
     5     if(isset($arr[$str[1]])){
     6         return $arr[$str[1]];
     7     }
     8     else{
     9         return $str;
    10     }
    11 }
    12 $string="书籍:<strong>php</strong> 出版社:<strong>邮电</strong>";
    13 $result=preg_replace_callback("/<strong>(.*)<\/strong>/U","strrep",$string);
    14 echo $result;        //书籍:PHP 函数大全 出版社:人民邮电出版社
    15 ?>
  6. preg_replace      执行正则表达式所匹配的搜索和替换
    1 <?php    //应用preg_replace()函数将关键字替换为*号
    2 if(isset($_POST['word'])){
    3     $key = array("/黄色/","/暴力/","/盗版/");                //过滤的关键字    
    4     $result = preg_replace($key, "*", $_POST['word']);     //将关键字替换为*号
    5     echo $result;
    6 }
    7 ?>
    1 <?php    //应用preg_replace()函数替换网址
    2 $string="爱玩C:http://www.iwanc.com";
    3 echo preg_replace("/http:\/\/(.*)/","<a href=\"\${0}\">\${0}</a>",$string);
    4 ?>
  7. preg_split   用正则表达式分割字符串
    1 <?php    //应用preg_split()函数分割词组
    2 if(isset($_POST['word'])){
    3     $array = preg_split("/\040|,|,|;|;/", $_POST['word']);    //以 空格 或 全角或半角 的 逗号和冒号 分割开
    4     $result = implode(" ",$array);
    5     echo $result;
    6 }
    7 ?>
  8. ereg_replace   替换字符串中含有正则表达式的部分
    1 <?php    //应用ereg_replace()函数在字符串中搜索正则表达式所有的匹配项并替换为指定的字符串
    2 $string="爱玩C:http://www.iwanc.com";
    3 echo ereg_replace("http://([A-Za-z.]+)$","<a href=\"\\0\">\\0</a>",$string);
    4 ?>
  9. ereg   用正则表达式校验字符串,返回 true 或 false
    1 <?php
    2 if(ereg("([PHP]+)","PHP 函数参考大全")){
    3     echo "匹配成功";
    4 }else{
    5     echo "匹配失败";
    6 }
    7 ?>
  10. eregi_replace   替换字符串中含有正则表达式的部分,但不区分大小写
    1 <?php    //应用eregi_replace()函数在字符串中搜索正则表达式所有的匹配项并替换为指定的字符串
    2 $string="爱玩C:http://www.iwanc.com";
    3 echo eregi_replace("http://([a-z.]+)$","<a href=\"\\0\">\\0</a>",$string);
    4 ?>
  11. eregi   用正则表达式校验字符串,但不区分大小写,返回 true 或 false
    1 <?php
    2 if(eregi("([Php]+)","PHP 函数参考大全")){
    3     echo "匹配成功";
    4 }else{
    5     echo "匹配失败";
    6 }
    7 ?>
  12. split   用正则表达式将字符串分割到数组中
    1 <?php    //应用split()函数通过正则表达式将字符串分割为数组
    2 $string="百度 And 谷歌 and 雅虎";
    3 $result=split("[a|A]+nd",$string);
    4 print_r($result);    //Array ( [0] => 百度 [1] => 谷歌 [2] => 雅虎 ) 
    5 ?>
  13. spliti   用正则表达式将字符串分割到数组中,但不区分大小写
    1 <?php    //应用spliti()函数通过正则表达式将字符串分割为数组
    2 $string="百度 And 谷歌 and 雅虎";
    3 $result=spliti("and",$string);
    4 print_r($result);    //Array ( [0] => 百度 [1] => 谷歌 [2] => 雅虎 )
    5 ?>
  14. sql_regcase   产生与字符串相匹配的正则表达式,不区分大小写
    1 <?php    //应用sql_regcase()函数转换一个字符串
    2 $string="爱玩C:http://www.iwanc.com";
    3 echo sql_regcase($string);    //爱玩[Cc]:[Hh][Tt][Tt][Pp]://[Ww][Ww][Ww].[Ii][Ww][Aa][Nn][Cc].[Cc][Oo][Mm]
    4 ?>
原文地址:https://www.cnblogs.com/iwanc/p/2603559.html