Showing posts with label preg_match. Show all posts
Showing posts with label preg_match. Show all posts

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 .

Wednesday, April 30, 2014

Preg Match Between two words

Extract words between two words Using Preg Match



we need the word  between "modules"  and  "widgets".

Please use this code.

Code :


$str = "addons/modules/featured/widgets/single/";

//$str = str_replace("/","-",$str);

preg_match('/modules\/(.*?)\/widgets/', $str, $match);

echo "<pre>";

print_r($match);

echo "</pre>";


Output :


Array
(
    [0] => modules/featured/widgets
    [1] => featured
)