Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用正则表达式进行特定域URL验证_Php_Regex - Fatal编程技术网

Php 使用正则表达式进行特定域URL验证

Php 使用正则表达式进行特定域URL验证,php,regex,Php,Regex,我一直在尝试自己,在网上搜索,写这个正则表达式,但没有成功 我需要验证给定的URL是否来自特定域和格式良好的链接(在PHP中)。例如: 好域名:example.com 来自example.com的优秀URL: 因此,不来自example.com的坏URL: 等等 一些注意事项: 我不关心“http”verus“https”,但如果它对您很重要,请假定“http”总是 将使用此正则表达式的代码是PHP,因此需要额外加分 2010年更新: Gruber添加了一个很棒的URL正则

我一直在尝试自己,在网上搜索,写这个正则表达式,但没有成功

我需要验证给定的URL是否来自特定域和格式良好的链接(在PHP中)。例如:

好域名:example.com

来自example.com的优秀URL:

因此,不来自example.com的坏URL:

  • 等等
一些注意事项: 我不关心“http”verus“https”,但如果它对您很重要,请假定“http”总是 将使用此正则表达式的代码是PHP,因此需要额外加分

2010年更新:

Gruber添加了一个很棒的URL正则表达式:

?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
(5)以下两种(a-z[a-z[[a-z[[[[w-[[w-[[[[[a-z0-10-9%[a-z0-10-10-10-10-9%[a-z[a-z[a-z[a-z[a-z[a-z[[a-a-z[[[[a-z[[[[[[[w-[w-z[[[a-z0-z0-z0-10-9-9-9-9-9-9-9-9-9%[9%[9%]]]]]]]]]]的[[[[[[[[[7 7 7 7 7 7 7 7 7 7 7 7 7.5.5]以下以下上述上述上述上述上述上述上述上述上述上述两两两两个[[[[[[[[[[[[[[[[[[[[[[[[[[[5]的代码>>>>>};:“,«»””)) 查看他的帖子:

我的刺

<?php

$pattern = "#^https?://([a-z0-9-]+\.)*blah\.com(/.*)?$#";

$tests = array(
    'http://blah.com/so/this/is/good'
  , 'http://blah.com/so/this/is/good/index.html'
  , 'http://www.blah.com/so/this/is/good/mice.html#anchortag'
  , 'http://anysubdomain.blah.com/so/this/is/good/wow.php'
  , 'http://anysubdomain.blah.com/so/this/is/good/wow.php?search=doozy'
  , 'http://any.sub-domain.blah.com/so/this/is/good/wow.php?search=doozy' // I added this case
  , 'http://999.sub-domain.blah.com/so/this/is/good/wow.php?search=doozy' // I added this case
  , 'http://obviousexample.com'
  , 'http://bbc.co.uk/blah.com/whatever/you/get/the/idea'
  , 'http://blah.com.example'
  , 'not/even/a/blah.com/url'
);

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

//  Here's another way
echo '<hr>';
foreach ( $tests as $test )
{
  if ( $filtered = filter_var( $test, FILTER_VALIDATE_URL ) )
  {
    $host = parse_url( $filtered, PHP_URL_HOST );
    if ( $host && preg_match( "/blah\.com$/", $host ) )
    {
      echo $filtered, " <strong>matched!</strong><br>";
    } else {
      echo $filtered, " <strong>did not match.</strong><br>";
    }
  } else {
    echo $test, " <strong>did not match.</strong><br>";
  }
}
也许:

^https?://[^/]*blah\.com(|/.*)$
编辑:

防止
http://editblah.com

^https?://(([^/]*\.)|)blah\.com(|/.*)$

你必须使用正则表达式吗?PHP有很多内置函数来做这类事情

filter_var($url, FILTER_VALIDATE_URL)
将告诉您URL是否有效,以及

    $domain = parse_url($url, PHP_URL_HOST);
将告诉您它所指的域


它可能比一些疯狂的正则表达式更清晰、更易于维护。

您的“好域名”示例不是有效的URL(缺少路径)。@Nikolar Ruhe:路径实际上是可选的:“http://”主机端口[“/”hpath[“?”搜索](请参阅RFC 1738)这并不是指一个有效的URL,而是指示例URL使用的有效域,但也许我应该只说'blah.com',而不是更多。不管怎样,我认为这一点是明确的。是好是坏?我认为这是允许的(假设a-Z是a-Za-Z)
parse_url
函数的文档声明它不是用来验证url的:无效的url可能仍然会被解析。所以你需要一些额外的检查。哦,我同意-它可能需要更严格的测试。尽管如此,我的正则表达式解决方案同样有效。我将你文章的逻辑应用到了我的第二个算法中。似乎工作得很好!聪明的彼得:)-正是我要找的。关闭!但这会对fooblah.com这样的域名产生误报
filter_var($url, FILTER_VALIDATE_URL)
    $domain = parse_url($url, PHP_URL_HOST);