http://localhost/ 的正则表达式 URL 匹配问题

发布于 2024-12-02 14:22:45 字数 724 浏览 1 评论 0原文

我正在尝试使用 GitHub https://github.com/ErisDS/Migrate 上的这个项目来迁移我的 Wordpress 数据库中的 URL 设置从本地主机开发安装到实时 URL。

目前,代码会抛出一个错误,要求将 URL 替换为“http://localhost/mysitename”,但确实接受新 URL“http://www.mywebsitename.com”

从我看来,错误来自于这个正则表达式没有将本地主机验证为有效的 URL - 有什么想法可以更新它以接受本地主机 URL?

完整代码可以在 GitHub 上查看。

function checkURL($url)
 {
  $url_regex = '/^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/';
if($url == 'http://')
 {
 return false;
 }    
return preg_match($url_regex, $url);
}

I am trying to use this project on GitHub https://github.com/ErisDS/Migrate to migrate the URL settings in my Wordpress database from a Localhost dev install to a live URL.

At the moment the code throws an error for the URL to be replaced "http://localhost/mysitename", but does accept the new URL "http://www.mywebsitename.com"

From what I can tell the error comes from this regular expression not validating the localhost as a valid URL - any ideas how i can update this to accept the localhost URL?

The full code can be viewed on GitHub.

function checkURL($url)
 {
  $url_regex = '/^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/';
if($url == 'http://')
 {
 return false;
 }    
return preg_match($url_regex, $url);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

司马昭之心 2024-12-09 14:22:45

您可以将“localhost”添加到可接受的主机名中,方法是将其更改为:

/^(http\:\/\/(?:[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}|localhost)(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/

这部分与 http:// 前缀匹配:

http\:\/\/

并且这部分与主机名匹配:

[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}

因此,您只需将主机名检查器更改为显式包含“localhost”的非捕获交替组:

(?:X|localhost)

其中 X 是现有主机名匹配子表达式。 (?: 位启动一个非捕获组,使用非捕获组可确保任何组编号引用都不会混乱。

还有一些实例:http://ideone.com/M0qqh

我认为一个简单的正则表达式可能会更好地为您服务,但它不能很好地处理 CGI 参数。你可以尝试这个:

/(http:\/\/[^\/]+\/([^\s]+[^,.?!:;])?)/

和看看它是否适用于您的数据。这个相当宽松,但对于一次性转换来说可能足够好:

'here is a URL http://localhost/x?a=b.'
'More http://example.com nonsense!.

您也可以尝试评论中的约瑟夫。

You can add "localhost" to the acceptable hostnames by changing it to this:

/^(http\:\/\/(?:[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}|localhost)(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/

This part matches the http:// prefix:

http\:\/\/

And this part matches the hostname:

[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}

So you can just change the hostname checker to a non-capturing alternation group that explicitly includes "localhost":

(?:X|localhost)

where X is the existing hostname matching sub-expression. The (?: bit starts a non-capturing group, using a non-capturing group ensures that any group number references won't get messed up.

And some live examples: http://ideone.com/M0qqh

I think a simple regex might serve you better though, that one doesn't deal with CGI parameters very well. You could try this:

/(http:\/\/[^\/]+\/([^\s]+[^,.?!:;])?)/

and see if it works for your data. That one is pretty loose but probably good enough for a one-shot conversion. That should properly match the URLs in these:

'here is a URL http://localhost/x?a=b.'
'More http://example.com nonsense!.

You could also try Joseph's from the comment.

浮生面具三千个 2024-12-09 14:22:45

它不起作用,因为在正则表达式中的某个位置您要求在 http:/// 之间添加一个点。 http://localhost/whatever 没有点,所以失败。

你真的应该使用类似 filter_var()parse_url() 而不是用于 URL 验证的正则表达式。

It's not working because somewhere in the regex you are asking for a dot in between http:// and /. http://localhost/whatever has no dot, so it fails.

You really should be using something like filter_var() or parse_url() instead of regexes for URL validation.

难忘№最初的完美 2024-12-09 14:22:45

MIGRATE 脚本的作者已更新 GitHub 上的项目以包含此修复。谢谢你们的帮助。

https://github.com/ErisDS/Migrate

The Author of the MIGRATE scripts has updated the project on GitHub to include this fix. thanks for your help guys.

https://github.com/ErisDS/Migrate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文