如何从字符串中删除http://删除(如果存在)?

时间:2014-02-03 19:51:58

标签: php string

我知道这是一件简单的事情,但由于我是一名php新手,我还没有理解如何删除 来自现有字符串的东西。

我想要一个名为“域名”的字符串,其中可能包含http://但可能没有http://,但{{1}}已被删除?

对于简单明了的任何帮助:)很好......

3 个答案:

答案 0 :(得分:3)

$str = 'http://www.google.com';
$str = preg_replace('#^https?://#', '', $str);
echo $str; // www.google.com

这将删除http和https:)。

答案 1 :(得分:1)

$d = 'http://stackoverflow.com';
$d = str_replace('http://', '', $d);

结果:

$d => 'stackoverflow.com';

答案 2 :(得分:0)

jQuery.validator.addMethod(“domaincheck”,function(val,elem){if(val.length == 0){return true;} if(!/ ^(https?| ftp):/// i .test(val)){val ='http://'+ val; $(elem).val(val);}