$url = preg_replace("/^http:/i", "https:", $url);
$url = str_replace( 'http://', 'https://', $url );
One way:
$url = '%s//google.com';$protocol = 'http:';if(!isset($_POST['https'])) { $protocol = 'https:';}$url = sprintf($url, $protocol);