preg_replace http with https

Q S*_*dio 3 php preg-replace

简而言之,我需要检查变量$ url中的字符串是否是一个简单的http,如果是这样,用https替换它 - 但我无法让它工作 - 任何想法:

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

干杯!

小智 15

为什么不str_replace呢?

$url="http://www.google.com"; // example http url ##
$url = str_replace('http://', 'https://', $url ); 
echo $url;
Run Code Online (Sandbox Code Playgroud)