Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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 - Fatal编程技术网

PHP自动更正URL

PHP自动更正URL,php,Php,我不想重新发明轮子,但我找不到任何图书馆能完美地做到这一点 在我的脚本中,用户可以保存URL,当他们给我列表时,我希望如下所示: google.com www.msn.com http://bing.com/ and so on... 我想能够保存在数据库中的“正确的格式” 我要做的是检查它是否存在协议,如果它不存在,我添加它,然后根据RegExp验证URL 对于PHP parse_url,任何包含协议的url都是有效的,所以它没有多大帮助 你们是怎么做到的,你们有什么想法想和我分享吗 编辑:

我不想重新发明轮子,但我找不到任何图书馆能完美地做到这一点

在我的脚本中,用户可以保存URL,当他们给我列表时,我希望如下所示:

google.com
www.msn.com
http://bing.com/
and so on...
我想能够保存在数据库中的“正确的格式”

我要做的是检查它是否存在协议,如果它不存在,我添加它,然后根据RegExp验证URL

对于PHP parse_url,任何包含协议的url都是有效的,所以它没有多大帮助

你们是怎么做到的,你们有什么想法想和我分享吗

编辑:

我想从用户输入中过滤出无效的URL(URL列表)。更重要的是,尝试自动更正无效的URL(例如,不包含协议)。当用户输入列表时,应该立即对其进行验证(没有时间打开URL检查它们是否确实存在)

从URL中提取部分会很好,比如
parse\u URL
do,但
parse\u URL
的问题是,它不能很好地处理无效的URL。我试图用它解析URL,对于缺少的部分(并且是必需的),我会添加默认的部分(例如,无协议,添加http)。但是“google.com”的
parse_url
不会将“google.com”作为主机名返回,而是作为路径返回

这对我来说似乎是一个非常常见的问题,但我在互联网上找不到可用的解决方案(找到了一些可以标准化URL的库,但如果URL无效,他们不会修复URL)

是否有一些“聪明”的解决方案,或者我应该坚持目前的做法:

  • 查找第一个出现的://并验证它之前的文本是否为有效协议,如果缺少,则添加协议
  • 找到下一次出现的/并验证主机名是否为有效格式
  • 为了更好地衡量,请通过RegExp整个URL再次验证

我只是觉得我会用这个拒绝一些有效的URL,对我来说最好是假阳性,假阴性。

这不是100%万无一失,而是一行

 $URL = (((strpos($URL,'https://') === false) && (strpos($URL,'http://') === false))?'http://':'' ).$URL;
编辑 如果主机名包含http,则我的初始版本显然存在问题


谢谢特伦特,它不是100%万无一失,而是一条1号班轮

 $URL = (((strpos($URL,'https://') === false) && (strpos($URL,'http://') === false))?'http://':'' ).$URL;
编辑 如果主机名包含http,则我的初始版本显然存在问题


感谢Trent我在解析url时遇到了与OP相同的问题,这是我自动更正url的快速而肮脏的解决方案(请记住,代码绝不完美或涵盖所有情况):

结果:
http:/ww.example.com/lorum.html=>http://www.example.com/lorum.html
地鼠:/ww.example.com=>gopher://www.example.com
http://www3.example.com/?q=asd&f=#asd=>http://www3.example.com/?q=asd&f=#asd
asd://.example.com/folder/folder/ =>http://example.com/folder/folder/
.example.com/=>http://example.com/
example.com=>http://example.com
subdomain.example.com=>http://subdomain.example.com
函数url\u解析器($url){
//多个///将解析url搞砸,将2+替换为2
$url=preg_replace('/(\/{2,})/','/',$url);
$parse\u url=parse\u url($url);
if(空($parse_url[“scheme”])){
$parse_url[“scheme”]=“http”;
}
if(空($parse_url[“host”])和&!empty($parse_url[“path”])){
//从路径的开头删除斜杠
$parse_url[“host”]=ltrim($parse_url[“path”],“\/”);
$parse_url[“path”]=“”;
}   
$return_url=“”;
//检查方案是否正确
if(!in_数组($parse_url[“scheme”],数组(“http”、“https”、“gopher”)){
$return_url.='http':/';
}否则{
$return_url.=$parse_url[“scheme”]。:/';
}
//检查是否设置了正确数量的“www”。
$explode_host=explode(“.”,$parse_url[“host”]);
//删除空条目
$explode\u host=阵列过滤器($explode\u host);
//并重新分配索引
$explode\u host=数组\u值($explode\u host);
//包含子域
如果(计数($explode\u host)>2){
//检查子域是否只包含字母w(然后不包含任何其他子域)。
if(substr_count($explode_host[0],'w')==strlen($explode_host[0])){
//替换为“www”以避免“ww”或“ww”等。
$explode\u主机[0]=“www”;
}
}
$return\u url.=内爆(“.”,$explode\u host);
如果(!empty($parse_url[“port”])){
$return_url.=“:”$parse_url[“port”];
}
如果(!empty($parse_url[“path”])){
$return_url.=$parse_url[“path”];
}
如果(!empty($parse_url[“query”])){
$return_url.='?'.$parse_url[“query”];
}
如果(!empty($parse_url[“fragment”])){
$return_url.='#'。$parse_url[“fragment”];
}
return$return\u url;
}
echo url_解析器('http:/ww.example.com/lorum.html');//http://www.example.com/lorum.html
echo url_解析器('gopher:/ww.example.com');//gopher://www.example.com
echo url_解析器('http:/www3.example.com/?q=asd&f=#asd');//http://www3.example.com/?q=asd&f=#asd
回显url_解析器('asd://.example.com/folder/folder/'); // http://example.com/folder/folder/
echo url_解析器('.example.com/');//http://example.com/
echo url_解析器('example.com');//http://example.com
echo url_解析器('subdomain.example.com');//http://subdomain.example.com

我在解析url时遇到了与OP相同的问题,这是我自动更正url的快速而肮脏的解决方案(请记住,代码决不是完美的,也不能涵盖所有情况):

结果:
http:/ww.example.com/lorum.html=>http://www.example.com/lorum.html
地鼠:/ww.example.com=>gopher://www.example.com
http://www3.example.com/?q=asd&f=#asd=>http://www3.example.com/?q=asd&f=#asd
asd://.example.com/folder/folder/ =>http://example.com/folder/folder/
.example.com/=>http://example.com/
example.com=>http://example.com
subdomain.example.com=>http://subdomain.example.com
函数url\u解析器($url){
//多个///将解析url搞砸,将2+替换为2
$url=preg_replace('/(\/{2,})/','/',$url);
$parse\u url=parse\u url($url);
if(空($parse_url[“scheme”])){
$parse_url[“scheme”]=“http”;
}
if(空($parse_url[“host”])和&!empty($parse_url[“path”])){
//从路径的开头删除斜杠
$parse_url[“host”]=ltrim($parse_url[“pa