Php 用于检查URL是否位于特定域下的正则表达式

Php 用于检查URL是否位于特定域下的正则表达式,php,regex,preg-match,Php,Regex,Preg Match,我需要一个正则表达式,它可以发现给定的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:dfd

我需要一个正则表达式,它可以发现给定的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.
-- 谢谢

这个怎么样

http:\/\/www\.my_domain\.com\/(.+)
它通过了您提供的四项测试

您可以这样做

 $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
 }

下面是您想要的模式

  $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>";
  }
$pattern=“#^https?://www.my_domain\.com(/.*)?$”;
$test='1http://www.google.com';
if(预匹配($pattern$test))
{
echo$test,“匹配!
”; }否则{ echo$test,“不匹配。
”; }
使用此

http:\/\/www\.my_domain\.com\/.*
你应该在发帖前表现出一些努力

  • http:\/\/
    -表示http://

  • www\.myu domain\.com
    等于www.mydomain.com

  • \/.
    表示/和0或表达式结尾的任何字符

嘿,伙计,我只是维诺·巴布……科瓦伊(儿童收养):)达。。therinju pochu:):)这可能不是正则表达式的工作,而是您选择的语言中的现有工具的工作。正则表达式并不是一根魔杖,你在遇到每一个涉及字符串的问题时都会挥舞它。您可能希望使用已经编写、测试和调试的现有代码。在PHP中,使用函数。Perl:。鲁比:。净: