regex是测试url的好方法吗?

时间:2022-09-06 18:47:07

I'm trying to test the validity of a url entered with php5. I thought of using regex, but assuming that it works correctly all the time, it only solves the problem of the url being syntactically valid. It doesn't tell me anything about the url being correct or working.

我正在测试一个与php5输入的url的有效性。我想到了使用regex,但是假设它一直都正常工作,它只解决了url语法有效的问题。它没有告诉我url是否正确或是否正常工作。

I'm trying to find another solution to do both if possible. Or is it better to find 2 separate solutions for this?

如果可能的话,我正在寻找另一种解决方案。还是最好找两个单独的解?

If a regex is the way to go, what tested regexes exist for urls?

如果要使用regex,那么对url有哪些已测试的regex ?

8 个解决方案

#1


11  

Instead of cracking my head over a regex (URLs are very complicated), I just use filter_var(), and then attempt to ping the URL using cURL:

我使用filter_var()而不是使用regex (URL非常复杂),然后尝试使用cURL来ping URL:

if (filter_var($url, FILTER_VALIDATE_URL) !== false)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($status_code >= 200 && $status_code < 400)
    {
        echo 'URL is valid!';
    }
}

#2


3  

For validation http://www.php.net/manual/en/filter.filters.validate.php

为验证http://www.php.net/manual/en/filter.filters.validate.php

For checking if it exists... well you need to try to access it actually.

检查它是否存在……你需要试着去访问它。

#3


2  

In order to test that a URL is 'correct or working', you'll need to actually try and interact with it (like a web browser would, for example).

为了测试URL是否“正确或有效”,您需要实际尝试并与它交互(例如,web浏览器会这样做)。

I'd recommend an HTTP library for Perl like LWP::Simple to do so.

我建议为Perl提供一个HTTP库,比如LWP:::这样做很简单。

#4


2  

RegExLib is good place to go for Reg Ex expressions

RegExLib是进行RegEx表达式的好地方

http://www.regexlib.com/Search.aspx?k=URL

http://www.regexlib.com/Search.aspx?k=URL

#5


1  

What I would do:

我想做的事:

  1. Check that the URL is valid using a very open regex or filer_var with FILTER_VALIDATE_URL.
  2. 使用带有FILTER_VALIDATE_URL的非常开放的regex或filer_var检查URL是否有效。
  3. Do an file_get_contents on the url and check that $http_response_header[0] contains a 200 HTTP resoponse.
  4. 在url上执行file_get_contents并检查$http_response_header[0]包含200个HTTP resoponse。

Now, that's dirty, sure there is some more elegant version using curl and stuff.

现在,这是脏的,肯定有一些更优雅的版本使用旋度和其他东西。

#6


1  

There are a bunch of 'check that an external file exists' functions on the file_exists() manual page.

在file_exists()手册页上有一堆“检查外部文件是否存在”函数。

#7


1  

i would use regex to go about solving this problem and i hate regex. This tool however makes my life so much easier... check it out >> http://gskinner.com/RegExr/

我将使用regex来解决这个问题,我讨厌regex。然而,这个工具让我的生活轻松多了……看看>> http://gskinner.com/RegExr/

#8


1  

Pinging a URL to see if it is a valid URL is nonsense!

点击一个URL来查看它是否是一个有效的URL是毫无意义的!

  • What if host is down?
  • 如果主机坏了怎么办?
  • What if the domain is not ping-able?
  • 如果域不能ping-able怎么办?

If you really want to do a "live" testing, better try to resolve the URL by using DSN. DNS is more reliable then PING or HTTP.

如果您真的想进行“实时”测试,最好尝试使用DSN解析URL。DNS更可靠,然后是PING或HTTP。

<?php
$ip = gethostbyname('www.example.com');

echo $ip;
?>

But even if this fails URL can be valid. It just have no DNS entry. So it depends on your needs.

但是即使这个URL失败了,它仍然是有效的。它只是没有DNS条目。这取决于你的需要。

#1


11  

Instead of cracking my head over a regex (URLs are very complicated), I just use filter_var(), and then attempt to ping the URL using cURL:

我使用filter_var()而不是使用regex (URL非常复杂),然后尝试使用cURL来ping URL:

if (filter_var($url, FILTER_VALIDATE_URL) !== false)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($status_code >= 200 && $status_code < 400)
    {
        echo 'URL is valid!';
    }
}

#2


3  

For validation http://www.php.net/manual/en/filter.filters.validate.php

为验证http://www.php.net/manual/en/filter.filters.validate.php

For checking if it exists... well you need to try to access it actually.

检查它是否存在……你需要试着去访问它。

#3


2  

In order to test that a URL is 'correct or working', you'll need to actually try and interact with it (like a web browser would, for example).

为了测试URL是否“正确或有效”,您需要实际尝试并与它交互(例如,web浏览器会这样做)。

I'd recommend an HTTP library for Perl like LWP::Simple to do so.

我建议为Perl提供一个HTTP库,比如LWP:::这样做很简单。

#4


2  

RegExLib is good place to go for Reg Ex expressions

RegExLib是进行RegEx表达式的好地方

http://www.regexlib.com/Search.aspx?k=URL

http://www.regexlib.com/Search.aspx?k=URL

#5


1  

What I would do:

我想做的事:

  1. Check that the URL is valid using a very open regex or filer_var with FILTER_VALIDATE_URL.
  2. 使用带有FILTER_VALIDATE_URL的非常开放的regex或filer_var检查URL是否有效。
  3. Do an file_get_contents on the url and check that $http_response_header[0] contains a 200 HTTP resoponse.
  4. 在url上执行file_get_contents并检查$http_response_header[0]包含200个HTTP resoponse。

Now, that's dirty, sure there is some more elegant version using curl and stuff.

现在,这是脏的,肯定有一些更优雅的版本使用旋度和其他东西。

#6


1  

There are a bunch of 'check that an external file exists' functions on the file_exists() manual page.

在file_exists()手册页上有一堆“检查外部文件是否存在”函数。

#7


1  

i would use regex to go about solving this problem and i hate regex. This tool however makes my life so much easier... check it out >> http://gskinner.com/RegExr/

我将使用regex来解决这个问题,我讨厌regex。然而,这个工具让我的生活轻松多了……看看>> http://gskinner.com/RegExr/

#8


1  

Pinging a URL to see if it is a valid URL is nonsense!

点击一个URL来查看它是否是一个有效的URL是毫无意义的!

  • What if host is down?
  • 如果主机坏了怎么办?
  • What if the domain is not ping-able?
  • 如果域不能ping-able怎么办?

If you really want to do a "live" testing, better try to resolve the URL by using DSN. DNS is more reliable then PING or HTTP.

如果您真的想进行“实时”测试,最好尝试使用DSN解析URL。DNS更可靠,然后是PING或HTTP。

<?php
$ip = gethostbyname('www.example.com');

echo $ip;
?>

But even if this fails URL can be valid. It just have no DNS entry. So it depends on your needs.

但是即使这个URL失败了,它仍然是有效的。它只是没有DNS条目。这取决于你的需要。