2012-05-10 3 views
3

간단히 말하면 변수 $ URL의 문자열이 단순한 http인지 확인해야합니다. 그렇다면 https로 바꾸십시오. 그러나 작동하지 않습니다 - 어떤 아이디어 :preg_replace http https가

$url="http://www.google.com"; // example http url ## 
$url_replaced = preg_replace('#^http://#','https://', $url); // replace http with https ## 

건배!

답변

12

왜 아니야 str_replace?

$url="http://www.google.com"; // example http url ## 
$url = str_replace('http://', 'https://', $url); 
echo $url; 
+0

둘은 그렇게 카로 얻을 .. 같은 시간에 대답 그/그녀가 덜 .. –

+1

은 그냥 URL이 다른 URL로 쿼리 PARAM을 포함하지 않는주의가 같은 점이다. 예 : 'echo str_replace ('http : //', 'https : //', 'http://foo.com?redirect=http://bar.com'); // https : //foo.com? redirect = https : // bar.com' – lizlux

3

preg_replace()은 여기에서 불필요합니다. str_replace() 만 사용하십시오.

str_replace('http://', 'https://', $url) 
1

언제나 안전한 것으로 링크를 반환하는 간단한 함수를 만들 수 있습니다. 링크를 많이 변경해야하는 경우 훨씬 쉽습니다.

function secureLink($url){ 

$url = str_replace('http://', 'https://', $url); 
return $url; 
};