将2个url图像拆分为一个

时间:2016-03-23 11:45:14

标签: php string function

我有json数据,我解析的内容。

{"response":[14516,{"id":310457,"from_id":-22759696,"to_id":-22759696,"date":1458731719,"post_type":"post","text":"Полиция Бельгии определила трех подозреваемых в теракте в аэропорту<br>.","attachment":{"type":"photo","photo":{"pid":405948296,"aid":-7,"owner_id":-22759696,"user_id":100,"src":"http:\/\/cs628628.vk.me\/v628628040\/41d81\/DciVVyiqqOU.jpg","src_big":"http:\/\/cs628628.vk.me\/v628628040\/41d82\/q2EumplfhhM.jpg","src_small":"http:\/\/cs628628.vk.me\/v628628040\/41d80\/zaETzTkDLUs.jpg","width":600,"height":340,"text":"Original: http:\/\/cdn15.img22.ria.ru\/images\/139461\/56\/1394615611.jpg","created":1458731718,"post_id":310457,"access_key":"62c1447c0497a750ce"}},"attachments":[{"type":"photo","photo":{"pid":405948296,"aid":-7,"owner_id":-22759696,"user_id":100,"src":"http:\/\/cs628628.vk.me\/v628628040\/41d81\/DciVVyiqqOU.jpg","src_big":"http:\/\/cs628628.vk.me\/v628628040\/41d82\/q2EumplfhhM.jpg","src_small":"http:\/\/cs628628.vk.me\/v628628040\/41d80\/zaETzTkDLUs.jpg","width":600,"height":340,"text":"Original: http:\/\/cdn15.img22.ria.ru\/images\/139461\/56\/1394615611.jpg","created":1458731718,"post_id":310457,"access_key":"62c1447c0497a750ce"}}],"comments":{"count":1},"likes":{"count":7},"reposts":{"count":0}}]}

我正在检索&#34; src_big&#34;:&#34; http://cs628628.vk.me/v628628040/41d82/q2EumplfhhM.jpg"例如。但是这个图像,而不是https://,我需要通过一组规则转换为https。

http://cs629401.vk.me/v629401896/3e689/pT5Z5oukzIE.jpg
https://pp.vk.me/c629401/v629401896/3e689/pT5Z5oukzIE.jpg

你可以看到我有类似的&#34; c629401&#34;我需要改变

  

http://cs629401.vk.me/

  

https://pp.vk.me/c629401/

此 图像可以使用其他链接,例如

http://cs628628.vk.me/v628628040/41d82/q2EumplfhhM.jpg
https://pp.vk.me/c628628/v628628040/41d82/q2EumplfhhM.jpg

1 个答案:

答案 0 :(得分:5)

使用preg_match的代码应该没问题:

<?php
    $imgurl = "http://cs629401.vk.me/v629401896/3e689/pT5Z5oukzIE.jpg";
    $toremove = Array("s", "S");
    preg_match("#http://(.+)\.vk\.me/(.+)#", $imgurl, $matches);
    $simgurl = "https://pp.vk.me/" . str_replace($toremove, "", $matches[1]) . "/" . $matches[2];

    echo $simgurl . "\n";

https://pp.vk.me/c629401/v629401896/3e689/pT5Z5oukzIE.jpg

您可以将其包装在函数中以转换网址:

<?php

function convertUrl($url)
{
    $toremove = Array("s", "S"); // Letters to remove from matches[1] group
    preg_match("#http://(.+)\.vk\.me/(.+)#", $url, $matches);
    return "https://pp.vk.me/" . str_replace($toremove, "", $matches[1]) . "/" . $matches[2];
}

echo convertUrl("http://cs629401.vk.me/v629401896/3e689/pT5Z5oukzIE.jpg") . "\n";

https://pp.vk.me/c629401/v629401896/3e689/pT5Z5oukzIE.jpg