2013-06-04 39 views
0

我需要一个正则表达式来查找给定的URL是否在我的网站下。正则表达式来检查URL是否在特定域

例如:

http://www.my_domain.com/internal_page should give true. 
www.my_domain.com/internal_page should give false. 
http://www.my_domain.com should give false. (should have a "/" after .com ). 
http://www.my_domain.com:dfdf should give false. 

- 谢谢

+0

**这可能不是一个正则表达式的工作,而是您选择的语言的现有工具。**正则表达式并不是一种魔术棒,它会在涉及到字符串的每一个问题上发挥作用。您可能想要使用已经编写,测试和调试的现有代码。 在PHP中,使用['parse_url'](http://php.net/manual/en/function.parse-url.php)函数。 Perl:['URI' module](http://search.cpan.org/dist/URI/)。 Ruby:['URI'' module](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI.html)。 .NET:['Uri'class](http://msdn.microsoft.com/en-us/library/txt7706a.aspx) –

回答

1

下面是你想要的图案,

$pattern = "#^https?://www.my_domain\.com(/.*)?$#"; 

    $test = 'http://www.google.com'; 

    if (preg_match($pattern, $test)) 
    { 
     echo $test, " <strong>matched!</strong><br>"; 
    } else { 
     echo $test, " <strong>did not match.</strong><br>"; 
    } 
+0

谢谢,它工作。 – Codeformer

+0

嘿,我是Vinoth巴布只...科瓦伊(儿童收养):) –

+0

Da .. therinju pochu :) :) – Codeformer

0

这个怎么样?

http:\/\/www\.my_domain\.com\/(.+) 

它通过你给

0

你可能只是这样做

$url = "http://www.my_domain.com/internal_page"; 
$host = "www.my_domain.com"; 
if(strpos($url, "http://".$host."/") === 0){ 
     // we have a winner 
} else { 
     // no good 
} 
1

使用此

http:\/\/www\.my_domain\.com\/.* 
四个测试

在发布之前,你应该真的表现出一些努力。

  • http:\/\/ - 指的http://

  • www\.my_domain\.com等于www.mydomain.com

  • \/.*装置/和0或表达的端部的任何字符。
相关问题