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 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)