banner ad

Regular Expression in PHP - preg_match | How to do in PHP | Forum

You must be logged in to post Login Register


Lost Your Password?

Search Forums:


 






Wildcard Usage:
*    matches any number of characters
%    matches exactly one character

Regular Expression in PHP – preg_match

UserPost

9:04 am
January 3, 2012


anki

Admin

posts 14

int preg_match
( string $pattern
, string $subject
[, array &$matches
[, int $flags = 0
[, int $offset = 0
]]] )

<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
Example #1 Find the string of text "php"

<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

 

Example #2 Find the word "web"

<?php
/* The b in the pattern indicates a word boundary, so only the distinct
 * word "web" is matched, and not a word partial like "webbing" or "cobweb" */
if (preg_match("/bwebb/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

if (preg_match("/bwebb/i", "PHP is the website scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>


About the CodeHelper.in forum

Forum Timezone: Africa/Abidjan

Most Users Ever Online: 14

Currently Online:
7 Guests

Currently Browsing this Topic:
1 Guest

Forum Stats:

Groups: 1
Forums: 9
Topics: 14
Posts: 18

Membership:

There are 82 Members

There is 1 Admin

Top Posters:

khasim – 2
vsreddy – 1
codehelper – 1

Recent New Members: gina123, khasim, vsreddy, codehelper, cindy2012car, sree

Administrators: anki (14 Posts)



banner ad