使用http:// upfront将网站的URL格式化为字符串

Abh*_*ari 4 php regex string url formatting

我有一个评论系统,允许自动链接网址.我正在使用cakephp,但解决方案更像是PHP.这是正在发生的事情.

如果用户输入完全合格的网址http://https://一切都很好.但如果他们进入www.scoobydoobydoo.com它就变成了http://cool-domain.com/www.scoobydoobydoo.com.基本上cakephp知道http | https是一个外部URL,所以它不能与http | https一起使用.

我的想法是在url上做一些str的东西,并让它插入http,如果不存在.不幸的是,无论我尝试什么只会让它变得更糟 我是菜鸟:)任何帮助/指针表示赞赏.

谢谢

编辑:发布解决方案片段.可能不是最好的,但多亏了答案,至少我有一些东西.

<?php
        $proto_scheme = parse_url($webAddress,PHP_URL_SCHEME);
    if((!stristr($proto_scheme,'http')) || (!stristr($proto_scheme,'http'))){
        $webAddress = 'http://'.$webAddress;
    }
?>
Run Code Online (Sandbox Code Playgroud)

Mic*_*nan 9

$url = "blahblah.com";
// to clarify, this shouldn't be === false, but rather !== 0
if (0 !== strpos($url, 'http://') && 0 !== strpos($url, 'https://')) {
   $url = "http://{$url}";
}
Run Code Online (Sandbox Code Playgroud)


Tob*_*ias 5

试试这个parse_url功能:http://php.net/manual/en/function.parse-url.php

我想这会对你有所帮助.