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];
}
Related
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.
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);
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>
Let's say I have a windows path name "E:\Dropbox\b\c\d", when I want to change this path to UNIX with some modifications, I can use this python code.
x = "E:\\Dropbox\\b\\c\\d"
print "/Users/prosseek/" + '/'.join(x.split('\\')[1:])
>> /Users/prosseek/Dropbox/b/c/d
What could be the equivalent code in PHP?
Here you go. You could use explode and implode, but there's no real reason to get fancy and use arrays when this is just a basic string operation:
$x = 'E:\Dropbox\b\c\d';
echo '/Users/prosseek/' . str_replace('\\', '/', substr($x, strpos($x, '\\') + 1));
$str = "E:\Dropbox\b\c\d";
$unix = str_replace('E:', '/Users/prosseek', $str);
$unix = str_replace('\\', '/', $unix);
demo
If you're looking for the exact PHP equivalent:
$x = 'E:\Dropbox\b\c\d';
$parts = explode('\\', $x);
array_shift($parts);
print "/Users/prosseek/" . implode('/', $parts);
However, this does not create the output you wanted. Demo.
A pretty much exact equivalent of that Python code would be:
$x = 'E:\\Dropbox\\b\\c\\d';
echo "/Users/prosseek" . implode('/', explode('\\', substr($x, 2)));
I have a string that looks like this, like a URL that has parameters.
folder/tested/file.js?p1=v1&p2=v2
How can I manipulate this string so as to remove all params, so that it ends up looking like this
folder/tested/file.js
Check out parse_url() - http://php.net/function.parse-url
$path = parse_url($url, PHP_URL_PATH);
$array = explode("?", "folder/tested/file.js?p1=v1&p2=v2");
$array[0];
There's no need for the explode workaround in this case:
$path = strtok($url, "?");
Here is another method that is somewhat 'dirty':
$tmp = 'folder/tested/file.js?p1=v1&p2=v2';
$pos = strpos($tmp, '?');
$url = substr($tmp, 0, $pos);
Try splitting by a '/' then by a '?' into two parts, and just taking what you need from both operations:
http://php.net/function.explode