This question already exists:
Closed 11 years ago.
Possible Duplicate:
How to remove parts in a URL?
I want to know how to remove first two parts of a URL in php?
I have a url called - http://localhost/photo/user-content/uploads/wall/o/74/ruveena.jpg
It means i want to remove - http://localhost/photo/
and I want make this URL to
user-content/uploads/wall/o/74/ruveena.jpg
I searched evrywhere but there only how to remove query part or host n those stuff i want to remove just only the first two parts of this url.
$url = parse_url('http://localhost/photo/user-content/uploads/wall/o/74/ruveena.jpg', PHP_URL_PATH);
$url = trim($url, '/');
$new_path = substr($url, strpos($url, '/') + 1);
Related
This question already has answers here:
Get parts of URL in PHP
(4 answers)
Closed 1 year ago.
I had previously been using basename to grab the last part of my URL however have noticed some issues if my URL contains parameters.
So if my URL is this:
https://www.google.com/test-page/?utm_source=test
How would I pull test-page from this?
You split it by the / delimiter, and then take the fourth item
$link = 'https://www.google.com/test-page/?utm_source=test';
$split = explode('/', $link);
if(isset($split[3]))
{
echo $split[3];
}
This question already has answers here:
Strip off specific parameter from URL's querystring
(22 answers)
Closed 4 years ago.
if I have some URL like these:
http://localhost/myfile.php?start=2018&end=2019&page=1&status=
Did anynone know how to replace any words from that link that contain page= ?
The result that I want is :
http://localhost/myfile.php?start=2018&end=2019&status=
Even if the link is like this:
http://localhost/myfile.php?start=2018&end=2019&status=&page=3
I still want the result will be without page variable and value, so it might be:
http://localhost/myfile.php?start=2018&end=2019&status=
any idea how to do this? Especially without regex (regex is my last option)
You can use parse_url(), parse_str() and http_build_query():
// Your URL
$str = 'http://localhost/myfile.php?start=2018&end=2019&page=1&status=';
// Get parts
$parts = parse_url($str);
// Get array of arguments
parse_str($parts['query'], $args);
// Remove unwanted index
unset($args['page']);
// Rebuild your URL
echo $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . http_build_query($args);
// Output: http://localhost/myfile.php?start=2018&end=2019&status=
I encouraged to read documentation or to print_r() vars of this sample code for a better understanding.
This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 5 years ago.
I am unsure if it's possible to simply do some replace inside php or if i should look at .htaccess for this? What would be the best solution to clean up this url as desired.
The reason why this is not a duplicate is because i dont have a static url, the id's and text will be changed depending on what post is clicked on the website.
i have tried to do this with .htaccess but it doesn't work for me since i fetch different titles and id's depending on what post users decide to watch. this is how my href looks like
<?php echo $row['postTitle'] ?>
I would like to know how i would change this url for example
www.website.com/index.php?page=viewpost&id=26&title=title%of%the%post
to
www.website.com/index.php?page=viewpost/26/title_of_the_post
you didn't really explain what you are trying to do but here's how to exactly what you ask for.
<?php
$url = "www.website.com/index.php?page=viewpost&id=26&title=title%of%the%post";
$parts = explode('?',$url);
$url = $parts[0] . '?page=' $parts[1] ;
unset($parts[0]);
unset($parts[1]);
foreach($parts as $key => $part) {
$url .= '/'.$part;
}
echo $url;
?>
This question already has answers here:
PHP function to get the subdomain of a URL
(29 answers)
Closed 6 years ago.
I am new to PHP world and I would like to be able to get any subdomain typed on the URL, for example if the URL is mike.myapp.com I would like to get "mike", if the URL is "james.myapp.com" I would like to get "james" and so on.
How this is done with PHP?
I've done that using this in the past:
$domain = 'myapp.com';
$sub = preg_replace('/\.' . preg_quote($domain) . '/i', '', $_SERVER['HTTP_HOST']);
This effectively gets rid of your domain and TLD and isolates the subdomain name.
You could just as well not using regex and use str_ireplace() replacing ".myapp.com" with an empty string.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Extracting the last segment on an URI
I want to get the last parameter of the url for eg.
http://www.youtube.com/embed/ADU0QnQ4eDs
I have a full url like above and i want to get only last parameter with the PHP which is
ADU0QnQ4eDs
Please help
Something like this?
echo end(explode("/", $url));
This will throw an error if strict error reporting is enabled.
To avoid this, split them up like this:
$parts = explode("/", $url);
echo end($parts);
There are functions for that.
$url = 'http://www.youtube.com/embed/ADU0QnQ4eDs';
$url_path = parse_url($url, PHP_URL_PATH);
$basename = pathinfo($url_path, PATHINFO_BASENAME);
// $basename is "ADU0QnQ4eDs"
See http://php.net/parse_url and http://php.net/pathinfo.
You have a pattern here that can be easily scanned:
$interestedIn = sscanf($url, 'http://www.youtube.com/embed/%s');
Don't make your live more complicated than it needs to be. Technically substr would work, too, but this one adds more context.