Tata2
Tata2

Reputation: 923

Function preg_match() returns nothing

I want to use preg_match() in my code, but the result is nothing ... (or null or empty ?)

$domain = "stackoverflow.com";

$uriToTest = "http://stackoverflow.com/";

$pattern = "/^http(s)?://(([a-z]+)\.)*".$domain."/";

echo preg_match($pattern, $uriToTest);

What is the problem?

Upvotes: 3

Views: 1560

Answers (5)

Danon
Danon

Reputation: 2973

If you were using T-Regx, then this exception would be thrown immediately:

$domain = "stackoverflow.com";
$uriToTest = "http://stackoverflow.com/";

try
{
    pattern("/^http(s)?://(([a-z]+)\.)*" . $domain . '/')->match($uriToTest);
}
catch (SafeRegexException $e) {
    echo $e->getMessage();    // `Unknown modifier '/'`
}

But also!! T-Regx can automatically add delimiters, so you can go

pattern("^http(s)?://(([a-z]+)\.)*" . $domain)->match($uriToTest);

and it would automatically add a suitable delimiter for you.

Upvotes: 0

Paul Dixon
Paul Dixon

Reputation: 300855

If you take a look at your pattern, it's this

/^http(s)?://(([a-z]+)\.)*stackoverflow.com/

The delimiter is used as a matching character, and if you had errors turned on, you'd get a "Unknown modifier" error. So first tip: TURN ERROR REPORTING ON!

To fix it, try using a different delimiter, e.g. {}, as it's easier to read than loads of leaning toothpicks...

{^http(s)?://(([a-z]+)\.)*stackoverflow.com}

The other problem is the dot in the $domain becomes a wildcard match - anytime you insert unknown data into a regex, get in the habit of using preg_quote to escape it, e.g.

$pattern = "{^http(s)?://(([a-z]+)\.)*" . preg_quote($domain, '{') . "}";

(Note - nice catch from stema in the comments: if you use a different delimiter, you must pass that preg_quote. It's clever enough to spot paired delimiters, so if you pass { it will also escape }.)

Upvotes: 3

M Shahzad Khan
M Shahzad Khan

Reputation: 935

  $domain = "stackoverflow.com";

  $uriToTest = "http://stackoverflow.com/";

  $pattern = "^http(s)?://(([a-z]+)\.)*" . $domain . "^";

  preg_match($pattern, $uriToTest, $matches);
  print_r($matches);

Upvotes: -1

MajorCaiger
MajorCaiger

Reputation: 1913

You need to escape your forward slashes and the . in the domain name

$domain = "stackoverflow.com";

$uriToTest = "http://stackoverflow.com/";

$escapedDomain = str_replace('.', '\.', $domain);

$pattern = "/^http(s)?:\/\/(([a-z]+)\.)*".$escapedDomain."/";

echo preg_match($pattern, $uriToTest);

Upvotes: 0

MDEV
MDEV

Reputation: 10838

You're most likely getting an error and preg_match is returning false, as you are not escaping your forward slashes in your expression. Either use something else like a # as the expression delimeter or escape any forward slashes to stop the parser from trying to end the expression (/ should be \/ - or change the / at either end to be #)

//Quick fix to yours
$pattern = "/^http(s)?:\/\/(([a-z]+)\.)*".preg_quote($domain,'/')."/";
//More legible fix
$pattern = '#^https?://(([a-z]+)\.)*'.preg_quote($domain,'#').'#';

Note that you don't need parenthesis around the s in https (unless you're hoping to capture it)

Upvotes: 0

Related Questions