Output a part of URL in php - 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]");

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.

How to get the ID from itunes url?

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

PHP: How can i read the second part of URL

How can I take out the part of the url after the view name
Example:
The URL:
http://localhost/winner/container.php?fun=page&view=eims
Extraction
eims
This is called a GET parameter. You can get it by using
<?php
$view = $_GET['view'];
If this is for a URL which is not part of your website (e.g. Not your domain), but you wish to parse it. Something like this will work
$url = "http://example.com/index.php?foo=bar&acme=baz&view=asdf";
$params = explode('?', $url)[1]; // This gets the text AFTER the ? Note: If using PHP 5.3 or less, this may not work. You would then need to split it into two lines with the [1] happening on $params.
$pairs = explode('&', $params);
foreach($pairs as $p => $pair) {
list($keys[$p], $values[$p]) = explode('=', $pair);
$splits[$keys[$p]] = $values[$p];
}
echo $splits['view'];
<?php echo $_GET['view']; ?> //eims
If that's current URL, the simple and rock solid approach is to use the filter functions:
filter_input(INPUT_GET, 'view')
Otherwise, you can use parse_url() with PHP_URL_QUERY as second argument. The resulting string can be split with e.g. parse_str().
Are sure you are writing this "echo $_GET['view'];" in the container.php file?
Maybe write why do you need that "view".

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>";

Return specific HREF attribute using Xpath query

Having a major brain freeze, I have the following chunk of code:
// Get web address
$domQuery = query_HtmlDocument($html, '//a[#class="productLink"]');
foreach($domQuery as $rtn) {
$web = $rtn->getAttribute('href');
}
Which obviously gets the entire href attribute, however I only want 1 specific attribute within the href. I.e. If the href is: /website/product1234.do?code=1234&version=1.3&somethingelse=blaah
I only want to return the variable for "version", so wish to only return "1.3" in my example. What's most efficient way to do this?
You could use parse_url and parse_str to extract that information.
Bingo! Thanks webdestroya, parse_str is exactly what I am after:
$string="/website/product1234.do?code=1234&version=1.3&somethingelse=blaah";
parse_str($string,$return);
$version = $return['version'];
echo "Version: " . $version;
Prints:
Version: 1.3

Categories