Get Parts of Url in PHP

PHP get URI parts of URL

Look into parse_url(). That will get you most of the way there. All you would need to do is remove the portion of the path that is part of your base URL.

print_r(parse_url('http://localhost/project/controller/action/param1/param2'));

Array
(
[scheme] => http
[host] => localhost
[path] => /project/controller/action/param1/param2
)

How to get first 3 parts of URL in PHP?

Try this,

<?php
$url = 'http://something.com/somebody/somegirls/whatever/';
$parts = explode('/', $url);
$new_url = $parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3].'/'.$parts[4].'/';
echo $new_url;
?>

OUTPUT

http://something.com/somebody/somegirls/

Get part of URL with PHP

You will use the parse_url function, then look at the path portion of the return.
like this:

$url='www.mydomain.com/thestringineed?data=1';
$components=parse_url($url);

//$mystring= end(explode('/',$components['path']));

// I realized after this answer had sat here for about 3 years that there was
//a mistake in the above line
// It would only give the last directory, so if there were extra directories in the path, it would fail. Here's the solution:
$mystring=str_replace( reset(explode('/',$components['path'])),'',$components['path']); //This is to remove the domain from the beginning of the path.

// In my testing, I found that if the scheme (http://, https://, ...) is present, the path does not include
//the domain. (it's available on it's own as ['host']) In that case it's just
// $mystring=$components['path']);

How extract part of an URL in PHP to remove specific part?

...

$url = 'http://www.domain.com/something/interesting_part/?somevars&othervars';
$parts = explode('/', $url);
echo $parts[4];

Output:

interesting_part

How do i get the part of the url in php

You could do this:

<?php

$url = "http://www.yoursite/one/two/three/drink.pdf";
$ex = explode('/', $url);

$arr = array_slice($ex, -3, 3);
$output = '/'.implode('/', $arr);
var_dump($output); // Outputs /two/three/drink.pdf

First, you use explode to break the url string into an array. Then use array_slice to get the last three elements of the array and finally implode to glue back the array elements using / as the glue.

Putting it all in one line would look like:

echo '/'.implode('/', array_slice(explode('/', $url), -3, 3));

Getting parts of a URL in PHP

PHP provides a parse_url function.

This function parses a URL and returns an associative array containing
any of the various components of the URL that are present.

This function is not meant to validate the given URL, it only breaks
it up into the above listed parts. Partial URLs are also accepted,
parse_url() tries its best to parse them correctly.


You can see the test cases executed here.

$urls = array(
"https://stackoverflow.com/users/test/login.php?q=san&u=post#top",
"/users/test/login.php?q=san&u=post#top",
"?q=san&u=post#top",
"login.php?q=san&u=post#top",
"/users/test/login?q=san&u=post#top",
"login?q=san&u=post#top"
);
foreach( $urls as $x ) {
echo $x . "\n";
var_dump( parse_url($x) );
}

Get Last Part of URL PHP

You can use preg_match to match the part of the URL that you want.

In this case, since the pattern is easy, we're looking for a forward slash (\/ and we have to escape it since the forward slash denotes the beginning and end of the regular expression pattern), along with one or more digits (\d+) at the very end of the string ($). The parentheses around the \d+ are used for capturing the piece that we want: namely the end. We then assign the ending that we want ($end) to $matches[1] (not $matches[0], since that is the same as $url (ie the entire string)).

$url='http://domain.example/artist/song/music-videos/song-title/9393903';

if(preg_match("/\/(\d+)$/",$url,$matches))
{
$end=$matches[1];
}
else
{
//Your URL didn't match. This may or may not be a bad thing.
}

Note: You may or may not want to add some more sophistication to this regular expression. For example, if you know that your URL strings will always start with http:// then the regex can become /^http:\/\/.*\/(\d+)$/ (where .* means zero or more characters (that aren't the newline character)).

Get part of url in php

use parse_url

<?php
$url = 'http://www.domain.com/44-Paris/blogs-for-travel/tour1.html';

$parse = parse_url($url);
$path = explode('/', $parse['path']);

echo $parse['scheme']."://".$parse['host']."/".$path[1]."/";
?>


Related Topics



Leave a reply



Submit