If you have a url like this: rapidshare.com/#!download|value1|values|value3
and you know the numbers how can you extract the value 1 and the value 3
Is there a way to extract the values between the 2nd and 3rd | and 3rd and 4th |
Thanks!
$array = explode('|', $url);
And use $array[$index];
$url = 'http://rapidshare.com/#!download|546l3|448704915|eu.heinelt.ifile_1.4.1-2_iphoneos-arm_fabius.deb|2915';
$urlparts = explode("|",$url);
will give you all the parts as array in $urlparts
Al of the above are 1 way of doing this, but if your script is always extracting data after your script name. as in: somepage.com/download.php?var1='val1'&var2='val2' the only relevant part for you is var1='val1'&var2='val2'
Luckily, PHP has a superglobal that can help. $_SERVER['QUERY_STRING'] . This way you can get an array of values, much cleaner and not have to worry that your explode seperator was being used on any other part of the URL.
$url = $_SERVER['REQUEST_URI'];
$values = explode("|", $url);
Related
how can I use php to get exactly the id from google play url.
Example:
Google Play Url: https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en
I want to get the com.zing.zalo . Thank you!
Simple Way
use preg_match or get the id from the url using $_GET['id'], if
you get this from url as other answer did.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
preg_match("/[^?]+(?:\?id=([^&]+).*)?/", "$Url", $matches);
echo $matches[1]; //com.zing.zalo
Working Example here Check online
The Longest way:
Simply you can use some PHP function to get it. Lets you have the following url.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
so what you need to explode the url using ? which is only one on the url.
$arr = explode("?", $Url);
From that array you need to store only the second part cause you need query string. So take only $arr[1]. Now explode again the $arr[1] with the & sign which is divide the rest of the url i mean $arr[1].
$arr2 = explode("&", $arr[1]);
Now you are all set, use another explode function to get the com.zing.zalo from the $arr2[0].
$idval = explode("=", $arr2[0]);
Result, Just echo the second part of the $idval array.
echo $idval[1]; //com.zing.zalo
Use $_GET['id'] to get the query string value of id
<?php
echo $_GET['id'];
?>
You could do this with regex: (?:\?id=)(.*)\b (I'm sure there's a more effective regex for this, but this accomplishes what you require)
preg_match('/(?:\?id=)(.*)\b/', 'https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en', $matches);
print_r($matches);
Returns:
Array
(
[0] => ?id=com.zing.zalo&
[1] => com.zing.zalo
)
Well sorry for the probably misleading title. Wasn't sure how to describe it better.
When accessing the status page I want to get the attached ID. But I don't want to use GET fields (wordpress makes /status?id=2134 to /status/?id=1234 - that's the only reason actually).
So this is my url
http://foo.bar.com/status/1234/
I want to get 1234
Okay fine. I could use something like $_SERVER["REQUEST_URI"] + trim() for example. Probably regex would be the key to get this job done since one could do something like /status/1234/foo/bar/baz/.. But I'm wondering if there is something builtin with PHP to get this part of the url.
Use the parse_url() function, and extract it:
$url = 'http://foo.bar.com/status/1234/';
$path = trim(parse_url($url, PHP_URL_PATH), '/');
$items = explode('/', $path);
$num = array_pop($items);
var_dump($num);
You can also use a regular expression, if that tickles your fancy:
$url = 'http://foo.bar.com/status/1234/';
$path = parse_url($url, PHP_URL_PATH);
preg_match('~/status/(?P<num>\d+)/?~', $path, $result);
$num = isset($result['num']) ? $result['num'] : null;
var_dump($num);
Try to parses a URL and returns an associative array containing any of the various components of the URL that are present using parse_url, explode it using explode and finally select status id using end
Try like this
$url = 'http://foo.bar.com/status/1234/';
$statusId = explode('/',trim(parse_url($url, PHP_URL_PATH), '/'));
print end($statusId);
Demo Ex http://ideone.com/34iDnh
trim- http://php.net/trim
explode-http://php.net/explode
parse_url-[1]: http://php.net/manual/en/function.parse-url.php
$url = explode('/', $articleimage);
$articleurl = array_pop($url);
I have used the above method to get the last part of a URL.Its working.But I want to remove the last part from the URL and display the remaining part.Please help me.Here I am mentioning the example URL.
http://www.brightknowledge.org/knowledge-bank/media/studying-media/student-media/image_rhcol_thin
Try this:
$url = explode('/', 'http://www.brightknowledge.org/knowledge-bank/media/studying-media/student-media/image_rhcol_thin');
array_pop($url);
echo implode('/', $url);
There is no need to use explode, implode, and array_pop.
Just use dirname($path). It's a lot more efficient and cleaner code.
Use the following string manipulation from PHP
$url_without_last_part = substr($articleimage, 0, strrpos($articleimage, "/"));
For Laravel
dirname(url()->current())
In url()->current() -> you will get current URL.
In dirname -> You will get parent directory.
In Core PHP:
dirname($currentURL)
after the array_pop you can do
$url2=implode("/",$url)
to get the url in a string
Change this:
$articleurl = array_pop($url);
Into this:
$articleurl = end($url);
$articleurl will then hold the last array key.
Missed the part where you want to remove the value, you can use the function key() to get the key and then remove the value using that key
$array_key = key($articleurl);
unset(url[$array_key])
Pretty simple solution add in the end of your code
$url = implode('/', $url);
echo $url;
Notice that array_pop use reference argument passing so array will be modifed implode() function does the opposite to explode function and connects array elements by first argument(glue) and returns the string.
It looks like this may be what you are looking for. Instead of exploding and imploding, you can use the parsing functions which are designed to handle exactly this kind of URL manipulation.
$url = parse_url( $url_string );
$result =
$url['scheme']
. "://"
. $url['host']
. pathinfo($url['path'], PATHINFO_DIRNAME );
Here's the simple way to achieve
str_replace(basename($articleimage), '', $articleimage);
For the one-liners:
$url = implode('/', array_splice( explode('/', $articleimage), 0, -1 ) );
$url[''] and enter the appropriate number
Let say I've this URL:
http://example.com/image-title/987654/
I want to insert "download" to the part between "image-title" and "987654" so it would look like:
http://example.com/image-title/download/987654/
help would be greatly appreciated! thank you.
Assuming your URIs will always be the same (or at least predictable) format, you can use the explode function to split the URI into each of its parts, and then use array_splice to insert elements into that array, and finally use implode to put it all back together into a single string.
Note that you can insert elements into an array by specifying the $length parameter as zero. For example:
$myArray = array("the", "quick", "fox");
array_splice($myArray, 2, 0, "brown");
// $myArray now equals array("the", "quick", "brown", "fox");
There are a number of ways to do this in PHP:
Split and reconstruct using explode(), array_merge, implode()
Using substring()
Using a regular expression
Using str_replace
Assuming all the url's conform to the same structure (image-title/[image_id]) i recommend using str_replace like so:
$url = str_replace('image-title', 'image-title/download', $url);
If however image-title is dynamic (the actual title of the image) i recommend splitting and reconstructing like so:
$urlParts = explode('/', $url);
$urlParts = array_merge(array_slice($urlParts, 0, 3), (array)'download', array_slice($urlParts, 3));
$url = implode('/', $urlParts);
Not very well formatted, but i think this is what you need
$mystr= 'download';
$str = 'http://example.com/image-title/987654/';
$newstr = explode( "http://example.com/image-title",$str);
$constring = $mystr.$newstr[1];
$adding = 'http://example.com/image-title/';
echo $adding.$constring; // output-- http://example.com/image-title/download/987654/
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);