• How to use Regular Expression in PHP

    PHP has a great interface to regex with using the preg functions. These functions expect the regex patterns you feed them to be framed by one delimiter character on each side. For instance, if you choose “#” as a delimiter, for the regex pattern \b\w+\b, this is the string you would feed to a preg function: ‘#\b\w+\b#’

    for the delimiter, any character can be choosen apart from spaces and backslashes. If your delimiter appears in the pattern, it needs to be escaped. The forward slash is a popular delimiter, and strangely so since it needs to be escaped in all sorts of strings having to do with file paths. For instance, to match http://, do you really want your regex string to look like ‘/http:\/\//’? Doesn’t ‘,http://,’ look better?

    Rare characters such as “#”, “@” or “%” are more sensible and fairly popular choices. The “#” is not good choice because it clashes with the # you use in comment mode. Efficient way is the plain comma (“,”) as it allows the actual regex to stand out. (With many delimiters, it can look like the delimiter belongs to the expression.) However, the comma delimiter often clashes with commas in quantifiers such as {2,10}. Other useful—and perhaps the most practical in most circumstances—is the tilde (“~”).

    The second thing to know about PHP regex is that you can append modifiers to the end of pattern strings. For instance, “i” makes the pattern case insensitive. The pattern string:
    ‘,\brob,i’ will match the bold letters in Robert and robber.
    Of course, in this case you could still modulate case sensitivity with inline case modifiers (?i) and (?-i).

    Basic Preg_Match Syntax

    $subject='Give me 10 eggs';
    $success = preg_match($pattern, $subject, $match);
    if ($success) {
    	echo "Match: ".$match[0]."<br />"; 
    	echo "Group 1: ".$match[1]."<br />"; 
    	echo "Group 2: ".$match[2]."<br />"; 
    Match: 10 eggs
    Group 1: 10
    Group 2: eggs

    This preg_match tests the pattern on the subject string, returning the match, which is “10 eggs”, then Group 1, which is “10″, then Group 2, which is “eggs”. Notice that the overall match is syntactically equivalent to “Group 0″, in tune with an idea presented in the section about capturing vs. matching: “The Match is Just Another Capture Group”.

    Basic Preg_Replace Syntax

    For straight replacements (for instance, replacing ’10′ with ’20′), you don’t need regex (though you can use it of course). In such cases, str_replace is said to be faster than the preg_replace regex function: $string=str_replace(’10′,’20′,’$string’);

    Preg_replace comes in when you need a regex to express the string to be replaced, for instance when you only want to replace ’10′ when it stands on its own, but not in 101 or File10.

    $subject='Give me 12 eggs then 12 more.';
    $newstring = preg_replace($pattern, "6", $subject);
    echo $newstring;
    The Output:
    Give me 6 eggs then 6 more.

    This code replaces “10″ with “20″ in the subject, returning: “Give me 20 eggs”.
    Note that as it makes one replacement after another, the regex engine keeps working on the original string—rather than switching to the latest version of the string. For instance, on the string ‘abcde’, $string=preg_replace(‘,(?=[a])\w,’,'a’,$string); will produce ‘aacde’, whereas it would produce ‘aaaaa’ if the engine was switching to the latest version of the string on the fly.

Comments on this post

Leave a Reply

  • Security Code :

    − 4 = one