验证url并转换为协议格式 [英] Validate url and convert into protocol format

查看:29
本文介绍了验证url并转换为协议格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 file_get_content($url),它不适用于从 www 开始的 url.

I am using file_get_content($url) which does not work with url starting from www.

所以我试图附加 http:// 并在用户输入的 url 格式不正确时转换为正确的格式.

So I am trying to append the http:// and converting into proper form if user entered url is not in correct form.

在此处查看演示

<?php

$url= 'www.google.com';
$pad = 'http://';
$cmp = 'www';
$prefix = substr($url , 0,2);
if($cmp == $prefix)
{
     echo str_pad($url, strlen($url)+3 ,"$pad",STR_PAD_LEFT);
}

?>

此代码不回显正确的网址.这里有什么问题吗?

This code does not echo correct url. Any issue here?

推荐答案

这就是你所需要的:

if (strpos($url, '://') === false) 
    $url = 'http://' . $url;

这篇关于验证url并转换为协议格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆