开发者

Strip a section out of a url in PHP

I have the following URL that is being returned to me from the Vzaar upload api:

https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f"

I need to be able to strip just the first "vz82b51a36989c422abc4db1734开发者_开发技巧208933a" out of the url. Is there any way to do this in PHP?

Thank you in advance for any help that you provide.


That string seems to appear twice in your URL (is it even a valid URL?). You also did not define any rules for obtaining it, so here I just take the 5th "part" separated by slashes.

<?php
    $url = 'https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f"';
    $parsed = parse_url($url);
    $parts = explode('/', $parsed['path']);
    echo $parts[5];
?>


What you do is take the string right after the "source" tag. Done by exploding.

$a = 'https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f';
$parts = explode('/', $a);
$i = 0;
for(; $i < count($parts); $i++)
    if($parts[$i] == 'source')
        break;
$i++;
echo $parts[$i];


If it's always between slashes, you can explode() on / and grab the n-th item.


Try this:

$str = 'https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f"';
$str = substr($str, strpos($str, 'source/') + 7);
echo substr($str, 0, strpos($str, '/'));

At this example, the position of 'source' in the URL is not necessary.


Is the string always at the exact same location? if so, how about something like...

<?php
  $url = 'https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f"';
  $url = parse_url($url);
  $url = explode('/',$url['path']);
  print_r($url[5]);
?>

The above code would print vz82b51a36989c422abc4db1734208933a


I know I'm late to the party here, but you shouldn't need to do that

The GUID will be returned to you in the XML response from the first step in the upload process.

http://developer.vzaar.com/docs/version_1.0/uploading/sign.html

See the example at the bottom, you can pull the GUId straight from there.

<?xml version="1.0" encoding="UTF-8"?>
<vzaar-api>
 <guid>vz7651d8c2558b46179531548224c87f84</guid>
  <key>vz7/651/source/vz7651d8c2558b46179531548224c87f84/${filename}</key>
  <https>false</https>
  <acl>private</acl>
  <bucket>vzaar_development_bucket</bucket>
  <policy>ewogICAgICAnZ ... JywgIF0KICAgICAgfQ==</policy>
  <expirationdate>2009-06-11T00:05:43.000Z</expirationdate>
  <accesskeyid>96ZODEDA709P5JNKI6X08U7PBQ31GUY8</accesskeyid>
  <signature>1ZwSGQjv4nrKUM1M/euO8FdxG20=</signature>
</vzaar-api>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜