0

I have the following Regex in PERL which I need to convert to PHP

if ($referrer_url =~ /\.$domain/) { }

I currently have the following in PHP to match it, but I'm not getting the same results:

if (preg_match("/\.$domain/", $referrer_url)) { }

Can anyone tell me if what I have is the same or if I'm mistaken? Thanks!

1
  • 1
    "I'm not getting the same results" - what is different with your results? Commented Feb 14, 2014 at 14:24

2 Answers 2

3

Im just guessing that your $domain probably contains .'s like mysite.com if that is the case you need to use preg_quote on the variable:

if (preg_match("/\.".preg_quote($domain, "/")."/", $referrer_url)) { }
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, the Perl code is closer to being equivalent to if (preg_match("/\.".$domain."/", $referrer_url)) { }, but it probably should be what you said.
2

If $domain is a regular string you may prefer to use strpos to Find the position of the first occurrence of a substring in a string. This would achieve the same result as using preg_quote with the benefit of being easier to read.

if (strpos($referrer_url, ".$domain") !== false) {
}

2 Comments

$domain is treated as a regex pattern in the Perl code, this is different, but this probably does what the Perl code should do.
Or just if ( strpos( $referrer_url, ".$domain" ) ) { ... Yes, to my eyes, your solution is more readable than the preg_match() option. +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.