简体   繁体   English

将Perl Regex转换为PHP

[英]Converting Perl Regex to PHP

I have the following Regex in PERL which I need to convert to PHP 我在PERL中有以下正则表达式,需要将其转换为PHP

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

I currently have the following in PHP to match it, but I'm not getting the same results: 我目前在PHP中具有以下匹配项,但没有得到相同的结果:

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

Can anyone tell me if what I have is the same or if I'm mistaken? 谁能告诉我我所拥有的是一样的还是我弄错了? Thanks! 谢谢!

我只是猜测您的$ domain可能包含。就像mysite.com,如果是这种情况,则需要在变量上使用preg_quote

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

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 . 如果$ domain是常规字符串,则您可能更喜欢使用strpos 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. 这将获得与使用preg_quote相同的结果,并具有易于阅读的优势。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM