How to get the ID from itunes url? - php

I need to get the item ID from the iTunes url and it used to work like this before itunes changed there url structure;
$musicid = 'https://itunes.apple.com/album/the-endless-bridge-single/id1146508518?uo=1&v0=9989';
$musicid=explode('/id',$musicid);
$musicid=explode('?',$musicid[1]);
echo $musicid[0];
But now iTunes has deleted the 'id' prefix in the url so the above code does not return the id anymore, does anyone know a solution?
old itunes url; https://itunes.apple.com/album/the-endless-bridge-single/id1146508518?uo=1&v0=9989
new itunes url; https://itunes.apple.com/album/the-endless-bridge-single/1146508518?uo=1&v0=9989

You would just explode on the fourth slash, grabbing the fifth segment.
Simply remove id from /id (so that you explode() on /), and check the sixth index instead of the second (with [5] instead of [1]):
<?php
$musicid = 'https://itunes.apple.com/album/the-endless-bridge-single/1146508518?uo=1&v0=9989';
$musicid = explode('/', $musicid);
$musicid = explode('?', $musicid[5]);
echo $musicid[0]; // 1146508518
This can be seen working here.
Hope this helps! :)

Use a regular expression:
<?php
$url = 'https://itunes.apple.com/album/the-endless-bridge-single/1146508518?uo=1&v0=9989';
preg_match('/album\/[^\/]+\/(\d+)\?/', $url, $matches);
var_dump($matches[1]);
Demo here: https://3v4l.org/tF9pS

Looks like you can simply use a combination of parse_url and basename, eg
$musicid = basename(parse_url($musicid, PHP_URL_PATH));
Demo ~ https://eval.in/895029

Related

Get only folder name from url in php without any file name with extension

I want to get foldername without any file name from a url in php?
My url is http://w3schools.com/php/demo/learningphp.php?lid=1348
I only want to retrieve the http://w3schools.com/php/demo from the url?
How to do this? Please help.
Try this,
$URL = 'http://w3schools.com/php/demo/learningphp.php?lid=1348';
$URL_SEGMENTS = explode("/", $URL);
foreach($URL_SEGMENTS as $Segment){
echo $Segment;
}
explode() will separate the string with / and provide an array. So you will have all url segments in foreach loop and you use it or store it in string or array.
After your comment let me show the script which will return your desire url.
$Desired_URL = $URL_SEGMENTS[2].'/'.$URL_SEGMENTS[3].'/'.$URL_SEGMENTS[4];
echo $Desired_URL;
If the url is stored in $url:
$url = 'http://w3schools.com/php/demo/learningphp.php?lid=1348';
You can do a preg_replace like so:
print(preg_replace('/\/[^\/]*$/', '', $url));
http://w3schools.com/php/demo
That regex means to replace everything from a / and all characters that are not / ... [^/]* ... to the end of the string ... $ ... with an empty string. Just delete them.

Output a part of URL in php

So, I have dynamically generated pages that follows the following format:
http://example.com/name/first_name/last_name/John%2C+Smith
What is the php code to output the last part of the url?
so, it becomes like "John, Smith".
Thank you so much.
EDIT:
I realized that the URL is ended with another / and the answers given below does not pick it up. What change should I make?
http://example.com/name/first_name/last_name/John%2C+Smith/
EDIT 2:
So, the link is dynamically generated as the following:
href="http://example.com/name/first_name/last_name/<?php echo $full_name ?>"
Split the url, get the last fragment then URL decode it:
<?
$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
$end=urldecode($end);
//go on using $end then
?>
You could do this with a regex.
echo preg_replace_callback('~.*/(.*)~',
function($matches) {
return urldecode($matches[1]);
},
'http://example.com/name/first_name/last_name/John%2C+Smith');
Regex demo: https://regex101.com/r/bE3bO5/1
Output:
John, Smith
Update:
echo preg_replace_callback('~.*/(.+)~',
function($matches) {
return rtrim(urldecode($matches[1]), '/');
},
'http://example.com/name/first_name/last_name/John%2C+Smith/');
You can use parse_url with second parameter PHP_URL_PATH
$url = urldecode("http://example.com/name/first_name/last_name/John%2C+Smith");
$arr = array_filter(explode('/',parse_url($url, PHP_URL_PATH)));
print_r(end($arr));
Edited:
As per requirement for dynamic url you can use
$url = urldecode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

php preg_match get everything after match in string

Looking for how to get the complete string in a URI, after the away?to=
My code:
if (isset($_SERVER[REQUEST_URI])) {
$goto = $_SERVER[REQUEST_URI];
}
if (preg_match("/to=(.+)/", $goto, $goto_url)) {
$link = "<a href='{$goto_url[1]}' target='_blank'>{$goto_url[1]}</a>";
The original link is:
https://domain.com/away?to=http://www.zdf.de/ZDFmediathek#/beitrag/video/2162504/Verschw%C3%B6rung-gegen-die-Freiheit-%281%29
.. but my code is cutting the string after the away?to= to only
http://www.zdf.de/ZDFmediathek
You know the fix for this preg_match function to allow really every character following the away?to= ??
UPDATE:
Found out, that $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING'] is already cutting the original URL. Do you know why and how to prevent that?
try use (.*) to get all after to=
$str = 'away?to=dfkhgkjdshfgkhldsflkgh';
preg_match("/to=(.*)/", $str, $goto_url);
echo $goto_url[1]; //dfkhgkjdshfgkhldsflkgh
Instead of extracting the URL with regex from the request URI you can just get it from the $_GET array:
$link = "<a href='{$_GET['to']}' target='_blank'>{$_GET['to']}</a>";

Remove some slug from url

I have difficulties in remove some slug from the url.
I have some urls like these below
http://domain1.com/so-section/upload/image1.jpg
http://domain2example.com/so-section/upload/image2.jpg
http://domain3place.com/so-section/upload/image3.jpg
http://domain4code.com/so-section/upload/image4.jpg
http://domain5action.com/so-section/upload/image5.jpg
http://domain6rack.com/so-section/upload/image5.jpg
Obviously, you will see domain are unsame, So I only would like to get "/so-section/upload/imagename.jpg" from the url. Would that be possible to remove whatever domain name come! Please help!
You can use parse_url and then get the path:
$url = parse_url("http://domain1.com/so-section/upload/image1.jpg");
echo $url["path"]; ///so-section/upload/image1.jpg
$pieces = explode('/', $url);
echo '/'. $pieces[4].'/'.$pieces[5].'/'.$pieces[6];
A simple solution to get everything after the domain name.
function GetJunk($url)
{
$start = explode("/",$url); //get everything between "/"
array_shift($start);array_shift($start);array_shift($start); //shift sections
$junk = implode("/",$start); //get everything after the new shifted sections
return $junk; // so-section/upload/image2.jpg
};
Then to use you simply:
echo GetJunk("http://domain2example.com/so-section/upload/image2.jpg");
Here is a working example: http://phpfiddle.org/main/code/4sr-zrm
Hope this helps!

URL validation detect movie title

how can me detect movie title from url..
http://www.mysite.com/2430-Moonrise-Kingdom.aspx
http://www.mysite.com/2405-Dark-Shadows.aspx
http://www.mysite.com/2415-Madagascar-3-Europes-Most-Wanted.aspx
I need to convert utl from:
http://www.mysite.com/2405-Dark-Shadows.aspx
to: "Dark Shadows" or "Dark-Shadows"
my code is:
$regexUrl = "/\/[0-9]{2,4}\-[a-zA-Z0-9\-\.]+\.aspx\/?";
echo preg_match($regexUrl, $_SERVER["REQUEST_URI"]);
Something seems to go wrong with your pattern at the end. Try this (note the last few characters):
/\/[0-9]{2,4}\-[a-zA-Z0-9\-\.]+\.aspx/
You'll also want to add a group in order to more easily grab the part that is the title.
/\/[0-9]{2,4}\-([a-zA-Z0-9\-\.]+)\.aspx\/?
Finally, here's a simplified version that won't break if the number at the beginning has one or more than four digits, or if the title has strange characters (perhaps it's not an English-language film).
\/\d+-(.+)\.aspx$/
$url = 'http://www.mysite.com/2415-Madagascar-3-Europes-Most-Wanted.aspx';
$url = basename($url, '.aspx');
$url = substr($url, 1+strpos($url, '-'));
$url = strtr($url, '-', ' ');
echo $url;
But if this is your site you should rather use this:
$id = (int)basename($url);
and load the title from DB. So even when your URL gets corrupted you can still load proper page.

Categories