希望这这是你在找什么。
$str = "//Hello World/theEnd!";
echo $str . "<br>";
$str = trim($str,"/");
$str = "http://" . $str;
echo $str;
如果你想获得幻想你可以把这是一个循环,以及给你http://Hello World/theEnd!
。
function addhttp($url) {
$url = trim($url,"/");
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
$add = "/media/images/CmsPageModuleDataItem/62/6260.0_gefro-suppennudeln.jpg";
$add1 = "//cdn.abc.de/images/100/3/7/1/5/0/8/371508F1.jpg";
$add2 = "http://s7.abc.com/is/image/LandsEnd/461469_FG16_LF_616";
echo "<br />";
echo addhttp($add);
echo "<br />";
echo addhttp($add1);
echo "<br />";
echo addhttp($add2);
这给你
http://media/images/CmsPageModuleDataItem/62/6260.0_gefro-suppennudeln.jpg
http://cdn.abc.de/images/100/3/7/1/5/0/8/371508F1.jpg
http://s7.abc.com/is/image/LandsEnd/461469_FG16_LF_616
使用'substr($ string,int $ start,[int $ length])' –
这很聪明...我检查一下。谢谢。 – cgee
由于你的字符串以//开头,你希望它以http://开头,为什么不直接连接'http:'和你的字符串呢? – Kulvar