http://localhost/ 的正则表达式 URL 匹配问题
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将“localhost”添加到可接受的主机名中,方法是将其更改为:
这部分与
http://
前缀匹配:并且这部分与主机名匹配:
因此,您只需将主机名检查器更改为显式包含“localhost”的非捕获交替组:
其中
X
是现有主机名匹配子表达式。(?:
位启动一个非捕获组,使用非捕获组可确保任何组编号引用都不会混乱。还有一些实例:http://ideone.com/M0qqh
我认为一个简单的正则表达式可能会更好地为您服务,但它不能很好地处理 CGI 参数。你可以尝试这个:
和看看它是否适用于您的数据。这个相当宽松,但对于一次性转换来说可能足够好:
您也可以尝试评论中的约瑟夫。
You can add "localhost" to the acceptable hostnames by changing it to this:
This part matches the
http://
prefix:And this part matches the hostname:
So you can just change the hostname checker to a non-capturing alternation group that explicitly includes "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:
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:
You could also try Joseph's from the comment.
它不起作用,因为在正则表达式中的某个位置您要求在
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()
orparse_url()
instead of regexes for URL validation.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