PHP study notes --php textbooks on regular expression examples of code (Abstract

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

<?php 
        /*POSIX extended regular expression functions*/
        /*---------------------------------------------------*/
        $ereg = '^[$][[:alpha:]_][[:alnum:]]*';
        ereg($ereg,'$_name',$register); //Matching and storage array (//eregi) case insensitive
        var_dump($register); //Display array structure
        
        /*---------------------------------------------------*/
        $ereg = 'tm';
        $str = 'hello,tm,Tm,tM';
        $rep_str=eregi_replace($ereg,'TM',$str); //A successful match is replaced by TM ereg_replace (case sensitive)
        echo $rep_str;
        
        /*---------------------------------------------------*/
        $ereg = 'is';
        $str = 'This is a register book.';
        $arr_str = spliti($ereg,$str); //Splits the string. According to the regular expression matches split (case sensitive)
        var_dump($arr_str);
        
        
        /*PCRE compatible regular expression functions -- in terms of efficiency or the grammar of PCRE is better than POSIX*/
        /*---------------------------------------------------*/
        $preg = '/\d{3,4}-?\d{7,8}/';
        $arr = array('043212345678','0431-7654321','12345678'); //Contains the elements of an array
        $preg_arr = preg_grep($preg,$arr);
        var_dump($preg_arr);
        
        /*---------------------------------------------------*/
        $str = 'This is an example!';
        $preg = '/\b\w{2}\b/';
        $num1 = preg_match($preg,$str,$str1); //Matching and returns the number of matching success, the success in an array is successful, stop
        echo $num1."<br />";
        var_dump($str1);
        echo "<br />";
        //The matching number and returns successfully, the match does not stop success into an array of success, until the end, to match all characters
        $num2 = preg_match_all($preg,$str,$str2); 
        echo $num2."<br />";
        var_dump($str2);
        
        /*---------------------------------------------------*/
        $str = '!,$,^,*,+,.,[,],\\,/,b,<,>';
        $str2='b';
        //Automatic will escape regular special characters, if there are parameters, character parameter characters are escaped
        $match_one = preg_quote($str,$str2); 
        echo $match_one;
        
        /*---------------------------------------------------*/
        $string = '[b]Bold[/b]';
        //Matching and replaced by a back reference
        $b_rst = preg_replace('/\[b\](.*)\[\/b\]/i','<b>$1</b>',$string);
        echo $b_rst;
        
        /*---------------------------------------------------*/
        function c_back($str)
        {
            //Reverse Quotes
            $str = "<font color=$str[1]>$str[2]</font>";
            return $str;
        }
        $string = '[color=blue]Blue[/color]';
        //The preg_replace_callback function to replace a character, replaced by the callback function. And almost preg_replace
        echo preg_replace_callback('/\[color=(.*)\](.*)\[\/color\]/U',"c_back",$string);
        
        /*---------------------------------------------------*/
        //Without this function of books, I write the sample to you!
        $preg = "/\s+/"; //Here also can be replaced"/\s{1,}/";
        $str = "I'm Chinese! I can eat";
        $arr = preg_split($preg,$str); //Find and resolution. -- there's a slip of the pen, previously seen friends sorry, has now been corrected
        var_dump($arr);
    ?>


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by King at October 24, 2013 - 1:14 AM