Strip a section out of a url in PHP - 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 "vz82b51a36989c422abc4db1734208933a" 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>

Related

Find a string that is not always the same [PHP]

I need help finding something in a variable that isn't always the same, and then put it in another variable.
I know that what I'm looking for has 5 slashes, it starts with steam://joingame/730/ and after the last slash there are 17 numbers.
Edit: It doesn't end with a slash, thats why I need to count 17 numbers after the fifth slash
Assuming what you're looking for looks something like this:
steam://joingame/730/11111111111111/
Then you could use explode() as a simple solution:
$gameId = explode('/', 'steam://joingame/730/11111111111111/');
var_dump($gameId[4]);
or you could use a regex as a more complex solution:
preg_match('|joingame/730/([0-9]+)|', 'steam://joingame/730/11111111111111/', $match);
var_dump($match[1]);
This splits the string into an array then return the last element as the game_id. It doesn't matter how many slashes. It will always return the last one.
$str = 'steam://joingame/730';
$arr = explode("/", $str) ;
$game_id = end($arr);
Following on from what DragonSpirit said
I modified there code so the string can look like
steam://joingame/730/11111111111111
or
steam://joingame/730/11111111111111/
$str = 'steam://joingame/730/11111111111111/';
$rstr = strrev( $str ); // reverses the string so it is now like /1111111111...
if($rstr[0] == "/") // checks if now first (was last ) character is a /
{
$nstr = substr($str, 0, -1); // if so it removes the /
}
else
{
$nstr = $str; // else it dont
}
$arr = explode("/", $nstr) ;
$game_id = end($arr);
Thanks for the help, I've found a solution for the problem. I'm going to post an uncommented version of the code on pastebin, becuase I couldn't get the code saple thing working here.
code

Get value from URL after the last /

I have looked around for this but can only find links and references to this been done after an anchor hashtag but I need to get the value of the URL after the last / sign.
I have seen this used like this:
www.somesite.com/archive/some-post-or-article/53272
the last bit 53272 is a reference to an affiliate ID..
Thanks in advance folks.
PHPs parse_url (which extracts the path from the URL) combined with basename (which returns the last part) will solve this:
var_dump(basename(parse_url('http://www.somesite.com/archive/some-post-or-article/53272', PHP_URL_PATH)));
string(5) "53272"
You can do this :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$id = substr(url, strrpos(url, '/') + 1);
You can do it in one line with explode() and array_pop() :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
echo array_pop(explode('/',$url)); //echoes 53272
<?php
$url = "www.somesite.com/archive/some-post-or-article/53272";
$last = end(explode("/",$url));
echo $last;
?>
Use this.
I'm not an expert in PHP, but I would go for using the split function: http://php.net/manual/en/function.split.php
Use it to split a String representation of your URL with the '/' pattern, and it will return you an array of strings. You will be looking for the last element in the array.
This will work!
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$pieces = explode("/", $url);
$id = $pieces[count($pieces)]; //or $id = $pieces[count($pieces) - 1];
If you always have the id on the same place, and the actual link looks something like
http://www.somesite.com/archive/article-post-id/74355
$link = "http://www.somesite.com/archive/article-post-id/74355";
$string = explode('article-post-id/', $link);
$string[1]; // This is your id of the article :)
Hope it helped :)
$info = parse_url($yourUrl);
$result = '';
if( !empty($info['path']) )
{
$result = end(explode('/', $info['path']));
}
return $result;
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$parse = explode('/',$url);
$count = count($parse);
$yourValue = $parse[$count-1];
That's all.

isolate the number exists at the end of the URI php

I have the following URI:
/belt/belts/fk/product/40P35871
And I want to retrieve the last content after the last /.
In this case is 40P35871.
How can I do this?
How about explode?
$elements = explode('/', $input);
$productId = end($elements);
Here's a different solution entirely. (and the simplest!)
Using basename
$var = "/belt/belts/fk/product/40P35871";
echo basename($var);
Output:
40P35871
You don't need regex for something simple like that. Consider using strrchr, documentation here
$lastcontent = substr(strrchr($uri, "/"), 1);
Considering this special case of $uri being a path, the best answer would be the one provided by Chtulhu.
basename will return the last part of a path, documentation here
$lastcontent = basename($uri);
Just like this
$str = '/belt/belts/fk/product/40P35871';
$arr = explode('/', $str);
$var = array_pop($arr);
var_dump($var);
or
$var = substr($str, strrpos($str,'/') + 1);
Try this
$result = preg_replace('%(/(?:[^/]+?/)+)([^/]+)\b%', '$2', $subject);
use this:
echo preg_replace('/[a-z0-9]$/i', '$1', $url);
this will give you the last position
note: but on this url only, query strings make this useless and use need to parse the url for the same first for this to work
Don't use regex. In this case you can act as the follow
myUrl = $_SERVER[REQUEST_URL];
$number = substr(strrpos(myUri,'/')+1);
You don't need regex.
Find the last content and get it using substr():
$lastcontent = substr(strrchr($uri, "/"), 1);

Remove URL regardless of format

Having a brain freeze...
Have a URL which may be in any of the formats :
http://url.com/stuff
url.com/somestuff
www.url.com/otherstuff
https://www.url.com/morestuff
You get the picture.
How do I remove the .com part to leave just the various 'stuff' parts ? For example, the above would end up :
stuff
somestuff
otherstuff
morestuff
You could achieve that using the following code:
$com_pos = strpos($url, '.com/');
$stuff_part = substr($url, $com_pos + 5);
Click here to see the working code.
This should do the trick for you!
<?php
$url = "http://url.com/stuff";
$querystring = preg_replace('#^(https|http)?(://)?(www.)?([a-zA-Z0-9-]+)\.[a-zA-Z]{2,6}/#', "", $url);
echo $querystring;
I submitted this answer because I'm not very fond of solutions using explode() to handle this. Maybe your query string contains more slashes so, you'd have to write exceptions for those cases.
You can use explode to make an array, then get the last element from the array.
$str = 'http://url.com/stuff';
$arr = explode('/', $str);
echo end($arr); // 'stuff'
$path = parse_url('http://url.com/stuff', PHP_URL_PATH);
If you leave the second parameter unspecified you can return an array including the domain etc.
Use explode function to divide the string.
<?php
$url = "http://url.com/stuff";
$stuff = explode("/", $url);
echo $stuff[sizeof($stuff) - 1];
?>
I used sizeof to access to last element.
preg_replace("/^(https?:\/\/)?[^\/]+/" ,"", $url);

How can php read this parameter

If I have a stored input string that looks like this
http://site.com/param1/value1
how can php extract value1?
I know how to extract parameters that look like this
http://site.com?param1=value1
but it doesn't work for the format I'm asking about.
Generally you could parse url with parse_url and then explode path by / , and than read second value in array.
You can use a simple string function combination:
$str = "http://site.com/param1/value1";
$tail = substr($str, strrpos($str, "/") + 1);
Or if it's not sure if there is a / somewhere in the string:
preg_match("#/(\w+)$#", $string, $match);
$tail = $match[1];
For the microoptimizers: this too will generally be faster as the array-explode() workaround.
Fast & Easy:
$url = "http://site.com/param1/value1";
$split_url = explode("/", $url);
$value = $split_url[3];
Looking at some php.net manuals you can easily find this function, that totaly fits your needs
strchr
$url = 'http://example.com/param1/value1';
list($param1, $value1) = array_slice(explode('/', $url), -2, 2);
This will give you param1 and value1 from the example stored in the variables $param1 and $value1.
Look up parse_URL that's the function you want
I would suggest a combination of Trickers and Toby Allens solution
$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', trim($path, '/'));
$value = $segments[2];
If you have multiple key-value-paris you can ensure with trim(), that the key is always even and the value always odd
$count = count($segments);
$result = array();
for ($i = 0; $i < $count; $i += 2) {
$result[$segments[$i]] = $segments[$i+1];
}

Categories