2017-08-18 19 views

回答

1

您可以使用下面的代碼

$re = '/^(https?)(\:?\/?\/?)/'; 
$str = 'https:/ 
https/ 
https// 
http// 
http:/'; 
$subst = '\\1://'; 

$result = preg_replace($re, $subst, $str); 

echo "The result of the substitution is ".$result; 

正則表達式

/^(HTTPS?)(:???/ /)/

匹配HTTP/HTTPS在第一組中,每隔一個可能性是在第二組
只需更換每次使用正確值的第二組
請參閱demo