Thursday, September 25, 2014

PHP search word in string

preg_match — Perform a regular expression match.

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

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

 <?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("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}

//OUTPUT : A match was found.

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

//OUTPUT : A match was not  found.
?>

Special  thanks to buriedunderground .

No comments:

Post a Comment