Home arrow PHP arrow Page 3 - Strings and Regular Expressions

PHP’s Regular Expression Functions (POSIX Extended) - PHP

Strings and regular expressions are among the basic tools that help programmers get their jobs done. This five-part article series covers how these are used in PHP. It is excerpted from chapter nine of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

TABLE OF CONTENTS:
  1. Strings and Regular Expressions
  2. Regular Expression Syntax (POSIX)
  3. PHP’s Regular Expression Functions (POSIX Extended)
  4. Regular Expression Syntax (Perl)
By: Apress Publishing
Rating: starstarstarstarstar / 1
June 17, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

PHP offers seven functions for searching strings using POSIX-style regular expressions: ereg(), ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and sql_regcase(). These functions are discussed in this section.

Performing a Case-Sensitive Search

Theereg()function executes a case-sensitive search of a string for a defined pattern, returningTRUEif the pattern is found, andFALSEotherwise. Its prototype follows:

boolean ereg(string pattern, string string [, array regs])

Here’s how you could useereg()to ensure that a username consists solely of lowercase letters:

<?php
    $username = "jasoN";
    if (ereg("([^a-z])",$username))
        echo "Username must be all lowercase!";
    else
        echo "Username is all lowercase!";
?>

In this case,ereg()will returnTRUE, causing the error message to output.

The optional input parameterregscontains an array of all matched expressions that are grouped by parentheses in the regular expression. Making use of this array, you could segment a URL into several pieces, as shown here:

<?php
    $url = "http://www.apress.com";

    // Break $url down into three distinct pieces:
    // "http://www", "apress", and "com"
    $parts = ereg("^(http://www)\.([[:alnum:]]+)\.([[:alnum:]]+)", $url, $regs);

    echo $regs[0];    // outputs the entire string http://www.apress.com
    echo "<br />";
    echo $regs[1];    // outputs http://www
    echo "<br />";
    echo $regs[2];    // outputs "apress"
    echo "<br />";
    echo $regs[3];    // outputs "com"
?>

This returns the following:

--------------------------------------------
http://www.apress.com

http://www
apress
com
--------------------------------------------

Performing a Case-Insensitive Search

Theeregi()function searches a string for a defined pattern in a case-insensitive fashion. Its prototype follows:

int eregi(string pattern, string string, [array regs])

This function can be useful when checking the validity of strings, such as passwords. This concept is illustrated in the following example:

<?php
    $pswd = "jasonasdf";
    if (!eregi("^[a-zA-Z0-9]{8,10}$", $pswd))
       
echo "Invalid password!";
    else
        echo "Valid password!";
?>

In this example, the user must provide an alphanumeric password consisting of eight to ten characters, or else an error message is displayed.

Replacing Text in a Case-Sensitive Fashion

Theereg_replace()function operates much likeereg(), except that its power is extended to finding and replacing a pattern with a replacement string instead of simply locating it. Its prototype follows:

string ereg_replace(string pattern, string replacement, string string)

If no matches are found, the string will remain unchanged. Likeereg(),ereg_replace()is case sensitive. Consider an example:

<?php
    $text = "This is a link to http://www.wjgilmore.com/.";
    echo ereg_replace("http://([a-zA-Z0-9./-]+)$", 
                  
"<a href="\\0">\\0</a>",
                      $text);
?>

This returns the following:

--------------------------------------------
This is a link to
<a href="http://www.wjgilmore.com/">http:// www.wjgilmore.com</a>.

-------------------------------------------- 

A rather interesting feature of PHP’s string-replacement capability is the ability to back-reference parenthesized substrings. This works much like the optional input parameterregsin the functionereg(), except that the substrings are referenced using backslashes, such as\0,\1,\2, and so on, where\0refers to the entire string,\1the first successful match, and so on. Up to nine back references can be used. This example shows how to replace all references to a URL with a working hyperlink:

$url = "Apress (http://www.apress.com)"; $url = ereg_replace("http://([a-zA-Z0-9./-]+)([a-zA-Z/]+)",
                    "<a href="\\0">\\0</a>", $url);
                  
echo $url;
// Displays Apress (<a href="http://www.apress.com">http://www.apress.com</a>)


Note  Althoughereg_replace()works just fine, another predefined function namedstr_replace()is actually much faster when complex regular expressions are not required.str_replace()is discussed in the later section “Replacing All Instances of a String with Another String.”


Replacing Text in a Case-Insensitive Fashion

Theeregi_replace()function operates exactly likeereg_replace(), except that the search for
patterninstringis not case sensitive. Its prototype follows:

string eregi_replace(string pattern, string replacement, string string)

Splitting a String into Various Elements Based on a Case-Sensitive Pattern

Thesplit()function divides a string into various elements, with the boundaries of each element based on the occurrence of a defined pattern within the string. Its prototype follows:

array split(string pattern, string string [, int limit])

The optional input parameterlimitis used to specify the number of elements into which the string should be divided, starting from the left end of the string and working rightward. In cases where the pattern is an alphabetical character,split()is case sensitive. Here’s how you would usesplit()to break a string into pieces based on occurrences of horizontal tabs and newline characters:

<?php
   
$text = "this is\tsome text that\nwe might like to parse.";
   
print_r(split("[\n\t]",$text));
?>

This returns the following:

--------------------------------------------
Array ( [0] => this is [1] => some text that [2] => we might like to parse. )

--------------------------------------------

Splitting a String into Various Elements Based on a Case-Insensitive Pattern

Thespliti()function operates exactly in the same manner as its sibling,split(), except that its pattern is treated in a case-insensitive fashion. Its prototype follows:

array spliti(string pattern, string string [, int limit])

Accomodating Products Supporting Solely Case-Sensitive Regular Expressions

Thesql_regcase()function converts each character in a string into a bracketed expression containing two characters. If the character is alphabetical, the bracket will contain both forms; otherwise, the original character will be left unchanged. Its prototype follows:

string sql_regcase(string string)

You might use this function as a workaround when using PHP applications to talk to other applications that support only case-sensitive regular expressions. Here’s how you would usesql_regcase()to convert a string:

<?php
   
$version = "php 4.0";
   
echo sql_regcase($version);
   
// outputs [Pp] [Hh] [Pp] 4.0
?>



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: