Broad Network


Number of Matches in PHP Regular Expression Scheme

Advanced PHP Regular Expressions – Part 1

Foreword: In this part of the series, I talk about the number of matches in the subject of the PHP regular expression matching scheme.

By: Chrysanthus Date Published: 28 Jan 2014

Introduction

This is part 1 of my series, Advanced PHP Regular Expressions. In this part of the series, I talk about the number of matches in the subject of the PHP regular expression matching scheme. I use a localhost web server, and PHP for the code samples. The word, “subject” in this series, is the string in which the regular expression finds the match. Note: the abbreviation for Regular Expression in this series is, regex.

You should have covered the professional series on PHP regular expression titled, PHP Regular Expressions. If you have covered that series and understood, you should be able to solve 80% of PHP regular expression problems in PHP, today.  By the time you complete this series, you would be able to solve much of the remaining 20%, today. Most of the time, you do not use this 20% of the material. However, occasionally, some of the principles of the 20% become crucial and you have no choice other than to use them.

Note: the output of the code samples of this series is to the browser.

Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, just contact me at forchatrans@yahoo.com .

Pre-Knowledge
Below is a link titled, “PHP Course”. It takes you to a page of a PHP Volume of free tutorials on PHP. The volume consists of a good number of series. You should click the link to see what you should have studied first, before reaching here. This series is part of the PHP advanced course, in the volume.

Difference Between Regular Expression and Pattern
There is not much difference between regular expression and pattern. Consider the example,
    
    /[crb]at/

The whole expression including the forward slashes is the regular expression. The content, [crb]at, within the forward slashes is the pattern.

The preg_match() Function
This is the main function to determine a match in PHP regular expression scheme. In simple terms the syntax is:

    int preg_match ( string $pattern , string $subject)

The function searches the subject for a match to the regular expression given in pattern string (e.g. "/[crb]at/"). The function returns 1 or zero. It returns 1 if it sees a match. It returns 0 if it does not see a match. There can be more than 1 matching sub-string in the subject. The preg_match() function stops searching after the first match. In case there is more than one match in the subject, the 1 returned is for the first match. The function can return FALSE if an error occurs in the evaluation (search process). For the preg_match() function, zero means no match and FALSE means error. Try the following code:

<?php

    $re = "/World/";
    $subject = "Hello World";

    $var = preg_match($re, $subject);
  
    if ($var == 1)
        {
            echo "Matched";
        }
    else if ($var == 0)
        {
            echo "Not Matched";
        }
    else
        {
            echo "Error Occurred";
        }

?>

The preg_match_all() Function
This function, as the name implies, looks for all the matches in the subject. It returns the number of matches. If there is no match, it returns zero. It returns FALSE if an error occurs in the evaluation process. In simple terms, the syntax is:

    int preg_match_all ( string $pattern , string $subject , array &$matches)

The first argument is the regex. The second is the subject. The third is the array, which holds all the matches (sub-strings in the subject that match the pattern.). It is a two-dimensional array. The first row will have the matched strings, in the order typed in the subject. Read and try the following code:

<?php

    $subject = "A cat is an animal. A rat is an animal. A bat is a creature.";

    $var = preg_match_all("/[cbr]at/", $subject, $matches);
    echo $var, '<br>';

    echo "<br />";
    echo $matches[0][0] . "<br />";
    echo $matches[0][1] . "<br />";
    echo $matches[0][2] . "<br />";

?>

The output is:

3

cat
rat
bat

That is it for this part of the series. We stop here and continue in the next part:

Chrys

Related Links

Basics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
Advanced Course
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Namespace
PHP Web Application

More Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message