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]
Related
This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you
This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 8 years ago.
This would be an example:
redirect
dynamic_word can be changed because it is dynamic. When click "redirect", dynamic_word will be extracted. So, how to extract it in redirect.php file ? Thanks !
Use $_GET to get parameters from an URL
<?php
$thatName = $_GET['q'];
echo $thatName;
Result
dynamic_word
If samitha's correct looking answer is incorrect then perhaps you mean you would like to extract the dynamic word from a string.
In that case you could do
<?php
$string = 'http://mywebsite.com/redirect.php&q=dynamic_word';
$ex_stirng = explode('&q=', $string);
$dynamic_word = $ex_string(1);
?>
Or even use the strstr function:
http://www.php.net/manual/en/function.strstr.php
This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 8 years ago.
It works
$url = parse_url('http://yabadaba.com/brand#asos');
echo $url['fragment'];
But it doesn't work
$url = parse_url($_SERVER['REQUEST_URI']);
echo $url['fragment'];
What is wrong? :(
That is because the parameter preceded by # will not reach the server-side script. You can get it only by Javascript.
Why does it work on the first case ?
That is because you are hard-coding it in the parse_url function.
This question already has answers here:
How to add http:// if it doesn't exist in the URL
(8 answers)
Closed 9 years ago.
I need a small part of helping, i can't remember the requlare exp. to tell me about its a URL or not, i need to tjeck about its starting whit http:// or not, befures if its not have http:// in start i need to put it into and if its have i need to do nothing.
Hobe sombardy can help me whit this queistion, and thanks a lot for helping.
Template 1:
Title 1
Template 2:
Title 2
Use preg_replace like this
$url = 'http://www.yahoo.com';
echo 'http://' . preg_replace( '~^http://~', '', $url );
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);