dreamer2014520 2012-03-26 16:23 采纳率: 0%
浏览 82
已采纳

将Rackspace Cloud文件从HTTP转换为HTTPS

I have a series of Rackspace Cloud Files CDN URLs stored which reference an HTTP address and I would like to convert them to the HTTPS equivalent.

Rackspace Cloud Files CDN URLs are in the following format:

http://c186397.r97.cf1.rackcdn.com/CloudFiles Akamai.pdf

And the SSL equivalent for this URL would be:

https://c186397.ssl.cf1.rackcdn.com/CloudFiles Akamai.pdf

The changes to the URL are (source):

  1. HTTP becomes HTTPS
  2. The second URI segment ('r97' in this example) becomes 'ssl'

The 'r00' part seems to vary in length (as some are 'r6' etc.) so I'm having trouble converting these URLs to HTTPS. Here's the code I have so far:

function rackspace_cloud_http_to_https($url)
{
    //Replace the HTTP part with HTTPS
    $url = str_replace("http", "https", $url, $count = 1);

    //Get the position of the .r00 segment
    $pos = strpos($url, '.r');

    if ($pos === FALSE)
    {
        //Not present in the URL
        return FALSE;
    }

    //Get the .r00 part to replace
    $replace = substr($url, $pos, 4);

    //Replace it with .ssl
    $url = str_replace($replace, ".ssl", $url, $count = 1);

    return $url;
}

This however does not work for URLs where the second segment is of a different length.

Any thoughts appreciated.

  • 写回答

2条回答 默认 最新

  • dongyi1015 2012-03-26 16:41
    关注

    Try this:

    function rackspace_cloud_http_to_https($url)
    {
        $urlparts = explode('.', $url);
    
        // check for presence of 'r' segment
        if (preg_match('/r\d+/', $urlparts[1]))
        {
            // replace appropriate segments of url
            $urlparts[0] = str_replace("http", "https", $urlparts[0]);
            $urlparts[1] = 'ssl';
    
            // put url back together
            $url = implode('.', $urlparts);
            return $url;
        }
        else
        {
            return false;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器