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.
Related
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 to check if a URL contains a query string
(4 answers)
Closed 3 years ago.
i want to create a link like the below:
<a href="'.$_SERVER["REQUEST_URI"].'?&action=approve&holiday='.$result["sequence"].'">
the $_SERVER["REQUEST_URI"] includes any $_GET variables already set, but i am not sure whether to put a ? or & after this in the href because $_SERVER["REQUEST_URI"] could already include a $_GET variable therefore it would need & and not a ?
Check if it includes '?' or not.
$extra = 'action=approve&holiday='.$result["sequence"];
$glue = (strpos($_SERVER["REQUEST_URI"], '?') === false) '?' : '&';
Then, you can use this:
echo '<a href="'.$_SERVER["REQUEST_URI"]. $glue . extra .'">';
But, if you don't need the current passed parameters in URL, you can use the way #Utkanos said
You need to build the URL in parts. All of the data you need is contained in the $_SERVER superglobal.
$_SERVER['REQUEST_SCHEMA'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'//...
PHP_SELF denotes the URI beyond the hostname, e.g. "/foo/bar.htm" in "mydomain.com/foo/bar.htm"
http://php.net/manual/en/reserved.variables.server.php
This question already has answers here:
Beautiful way to remove GET-variables with PHP?
(12 answers)
How to remove content from url after question mark. preg_match or preg_replace?
(2 answers)
Closed 8 years ago.
I have this url: http:www.blabla.com/x/x/x/x?username=testuser
I need a string to read this url, but forget everything and including the ? mark.
So it becomes this: http:www.blabla.com/x/x/x/x
The reason for this is because I am making this variable:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
And this code:
if($host == "http:www.blabla.com/x/x/x/x") {
echo "lul";
}
But right now, the URL changes depending on what user is on, and it has to execute the echo no matter what user is on.
So I read some reges and preg_match etc. and I just wanted to hear your opinions or advice. How would I accomblish this the best? thanks!
This is too trivial of a task for regex.
$host = $_SERVER['SERVER_NAME'] . explode("?", $_SERVER['REQUEST_URI'], 2)[0];
(Note: this assumes you're up-to-date, or at least using PHP 5.4, for the dereference to work without a temporary variable)
Or if you must omit the get / request section just explode ? and use $host[0]
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.
This question already has answers here:
parse youtube video id using preg_match [duplicate]
(10 answers)
Closed 9 years ago.
So I've got this RSS file that I'm trying to get part of a URL from. So here's what I tried (which is not working).
I've got this URL I can get easily enough:
http://www.youtube.com/watch?v=tUPjxGmh9i8&feature=youtube_gdata
I tried doing an $link = ltrim($link, 'http://www.youtube.com/watch?v='); in PHP and an $link = rtrim($trim, '&feature=youtube_gdata');
And it returned "UPjxGmh9i8". It cuts off the "t" in the front. The PHP.net documentation pages aren't the easiest for me to read and interpret, but I'm assuming that any individual character within the second parameter of ltrim() and rtrim() is taken out and this is not what I want. Is there some other solution I can use to grab only the text I want?
Any help is greatly appreciated.
This is how I would do it...
$query = parse_url(
'http://www.youtube.com/watch?v=tUPjxGmh9i8&feature=youtube_gdata',
PHP_URL_QUERY);
parse_str($query, $params);
$slug = get_magic_quotes_gpc() ? stripslashes($params['v']) : $params['v'];
CodePad.
The reason trim() (or its variants) won't work is because it accepts a character list (ordinal not significant), of which http://www.youtube.com/watch?v= contains t. It does not accept a string to remove.
Using PHP, cant you just grab the value of v?
$result = $_GET['v'];
var_dump($result);