Get the dynamic number after the last "/" of external url - php

This question was migrated from WordPress Development Stack Exchange because it can be answered on Stack Overflow.
Migrated 3 days ago.
i want to grab the last dynamic numbers of an external url. I m not good in php, can someone help?
https://data.aboss.com/v1/agency/1001/101010/events/xxxxxx
$url = https://data.aboss.com/v1/agency/1001/101010/events/;
$value = substr($url, strrpos($url, '/') + 1);
value="<?php echo $value; ?>"
result should be xxxxxx.

There are many ways to do this, the simplest one-liner would be to use explode:
echo explode("/events/", $url)[1];
Or if something may come after 'events/xxxxxx':
$url = "https://data.aboss.com/v1/agency/1001/101010/events/1524447/other/123456";
echo explode("/", explode("/events/", $url)[1])[0]; // 1524447
You can also use a regex with preg_match:
$url = "https://data.aboss.com/v1/agency/1001/101010/events/1524447/other/123456";
$matches = [];
preg_match("~/events/(\d+)~", $url, $matches); // captures digits after "/events/"
echo($matches[1]); // 1524447
Sandbox

To get the last part from the url, you can use parse_url() and pathinfo() function in PHP. Try following code,
<?php
$url = "https://data.aboss.com/v1/agency/1001/101010/events/xxxxxx";
$path = parse_url($url, PHP_URL_PATH);
$value = pathinfo($path, PATHINFO_FILENAME);
echo $value;

Related

How can I get an URL's host without schema and ending in php?

I want to explode urls to get the host.
$link = str_replace("www.", "", $_POST['link']);
$part1 = explode("//", $link);
$part2 = explode(".", $part1[1]);
$host = $part2[0];
So if the $_POST['link'] is e.g.
https://www.youtube.com/watch?v=udm5jUA-2bs
I want to get just youtube.
In this first way I get explode() expects parameter 2 to be string, array given" error message.
With this method:
$host = var_dump(parse_url($_POST['link'], PHP_URL_HOST));
I get www.youtube.com but I want only youtube.
Extract the url domain using parse_url() function with mode PHP_URL_HOST.
We will use explode() function to break it into parts using . as delimiter.
Now, the URL can be of two types, either youtube.com, or www.youtube.com, or %.youtube.com
We count the parts, and if they are two, we use the first value, else the second value.
DEMO: https://3v4l.org/KlErW
$url_host_parts = explode('.', parse_url($_POST['link'], PHP_URL_HOST));
$host = (count($url_host_parts) == 2) ? $url_host_parts[0] : $url_host_parts[1];
what about this without any regex? You can use parse_url() and explode() to get what you want i.e youtube
<?php
$url = 'https://www.youtube.com/watch?v=udm5jUA-2bs';
$parse = parse_url($url);
//print_r($parse);
echo explode('.',$parse['host'])[1];
?>
Output:
youtube
DEMO: https://3v4l.org/kG1nD
You can use regex in preg_match() to do this work.
$url = "https://www.youtube.com/watch?v=udm5jUA-2bs";
preg_match("/(\w+)(?=\.\w{2,}(\/|\?|$))/", $url, $matches);
echo $matches[1]; // youtube
Check result in demo

How to get part from URL

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

Remove characters from end of URL

Say I have a URL with something like this:
http://website.com/website/webpage/?message=newexpense
I have the following code to try and get the the URL before the question mark:
$post_url = $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link_before_question_mark = explode('?', $actual_link);
$add_income_url = $link_before_question_mark[0];
In this example I would get the following URL:
http://website.com/website/webpage/
I'd like to remove the webpage portion of this so the URL is:
http://website.com/website/
How can I do this?
Use parse_url This way you have all components.
$url = 'http://website.com/website/webpage/?message=newexpense';
$pUrl = parse_url( $url);
echo $pUrl['scheme'] . '://' . $pUrl['host'] . $pUrl['path'];
You can do a similar trick using explode. Then pop the parts you don't need and implode the url back together. If you are sure that the part after '?' never contains a '/', you can replace your code with this one. If you're not sure, you should first remove the part after '/' and then run this code to remove the last part of the path.
<?php
$url = 'http://website.com/website/webpage/?message=newexpense';
$parts = explode('/', $url);
// Remove the last part from the array
$lastpart = array_pop($parts);
// If the last part is empty, or the last part starts with a '?'
// this means there was a '/' at the end of the url, so we
// need to pop another part.
if ($lastpart == '' or substr($lastpart, 0, 1) == '?')
array_pop($parts);
$url = implode('/', $parts);
var_dump($url);
I'd probably use dirname; it's specifically designed to strip the last stuff after a "/"...
$url = "http://website.com/website/webpage/?message=newexpense";
echo dirname(dirname($url))."/"; // "http://website.com/website/"
(As it says in the documentation, "dirname() operates naively on the input string, and is not aware of the actual filesystem...", so it's quite safe to use for this kind of purpose.)
Try it with explode
<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$link_before_question_mark = explode('?', $actual_link);
$add_income_url = $link_before_question_mark[0];
$split=explode('/', $add_income_url);
echo $split[0]."//".$split[2]."/".$split[3]."/";
?>
Even better is...
<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$split=explode('/', $actual_link);
echo $split[0]."//".$split[2]."/".$split[3]."/";
?>

PHP Get last word of url returning blank [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP Get end string on url between / and /
I have this code to get the last part of the url between / and / at the end.
The url is:
http://mydomain.com/category/mobile/
I need to retrieve "mobile" from it.
The code i'm using is:
$url = trim($url, '/');
//Put it in a variable
$the_category = substr($url, strrpos($url, '/'));
//whats in it?
echo $the_category;
The above returns nothing ... any ideas?
You should explode an array using "/" as the delimiter and then get the last value of the array, like so:
<?
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
$array = explode('/',$url);
$the_category = end($array);
//what's in it?
echo $the_category;
?>
How about:
$the_category = basename($url);

Replace string using php preg_replace

Hi all i know preg_replace can be used for formatting string but i need help in that concerned area my url will be like this
http://www.example.com/index.php/
also remove the http,https,ftp....sites also
what i want is to get
result as
example.com/index.php
echo preg_replace("~(([a-z]*[:](//))|(www.))~", '', "ftp://www.example.com");
$url = 'http://www.example.com/index.php/';
$strpos = strpos($url,'.');
$output = substr($url,$strpos+1);
$parts=parse_url($url);
unset($parts['scheme']);
//echo http_build_url($parts);
echo implode("",$parts);
EDIT
To use http_build_url you needs pecl_http you can use implode as alternate
Something like this
$url = "http://www.example.com/index.php";
$parts = parse_url($url);
unset($parts['scheme']);
echo preg_replace('/^((ww)[a-z\d][\x2E])/i', '', join('', $parts));
Output
example.com/index.php
Example #2
$url = "http://ww3.nysif.com/Workers_Compensation.aspx";
Output
nysif.com/Workers_Compensation.aspx

Categories