首先,我需要检查URL字符串,如果URL的协议是https
,那么我需要在PHP中替换http
。因此,这个php函数的输入和输出必须如下所示:
Input -> https://example.com/example/https.php
Output-> http://example.com/example/https.php
Input -> http://example.com/example/https.php
Output-> http://example.com/example/https.php
这将确保它位于字符串的开头,然后是://
$input = 'https://example.com/example/https.php';
echo preg_replace('/^https(?=:'/'/)/i','http',$input);
function remove_ssl ($url) {
if (strpos($url, 'https://') == 0) {
$url = 'http://' . substr($url, 7);
}
return $url;
}
strpos($url, 'https://') == 0
是故意的,而不是===,因为我们只希望URL以https://开头时出现这种情况,并将其替换掉。
另请参阅:http://php.net/manual/en/function.parse-url.php
...
$parsed_url = parse_url($url);
if ($parsed_url['scheme'] == 'https') {
$url = 'http://' . substr($url, 7);
}
return $url;
...
首先需要用strpos()
:检查https
的存在
if( strpos( $url, 'https://') === 0){
(注意===
),然后您可以使用substr()
:提取https://
之后的所有字符串(即前8个字符之后,或保留原始://
时为5个字符之后)
$url = 'http://' . substr( $url, 8);
$parse = parse_url($url);
if($parse['scheme'] === 'https')
{
$url = str_replace('https','http',$url,1);
}
您可以使用此解决方案。
您可以使用str_pos()
和str_replace()
的组合来实现这一点:
if(str_pos($input,'https:') === 0) {
$output = str_replace('https:','http:',$input,1);
} else {
$output = $input;
}
function replace_uri_protocol($uri, $search, $replacement){
$parts = parse_url($uri);
$uri = $replacement."://".$parts["host"].$parts["path"];
if(isset($parts["query"])){
$uri .= "?".$parts["query"];
}
return $uri;
}
试试这个吧,它对我有用…
$url = 'https://www.example.com';
echo preg_replace("(^https?://)", "http://", $url);