Query string in php code - php

How to put the querystring name in php?
$file_get_html('http://localhost/search/?q=');
And when accessing localhost/?name=example the code looks like this
$file_get_html('http://localhost/search/?q=example');
I do not know how to put $_GET['url'] inside a php :(

The question isn't very clear, but I suspect this is the answer:
file_get_html('http://localhost/search/?q=' . urlencode($_GET['url']));

The ?q=example will let you use something like $example = $_GET['q'],
and $example should equal the value of q in your querystring.
If you have a querystring that looks like this:
?q=example&url=myurl.com
You can access the q and url parameters like this:
$q = $_GET['q']; // which will equal 'example'
$url = $_GET['url']; // which will equal 'myurl.com'
If that is what you are trying to do.

$url = urlencode($_GET['url']);
$contents = file_get_contents("http://localhost/search/?q={$url}");
var_dump($contents);

Related

Replace url paramer by using php

I have a url & its structure like
https://www.example.com/i/location-name/category/subcategory/item/item-id
I want to replace the url parameter(location-name) dynamically by new parameter using PHP.
Modified url is looks like this
https://www.example.com/i/new-parameter/category/subcategory/item/item-id
I had successfully done with query parameters by using http_build_query();
But in this case i had tried with preg_replace(), but its not working
Thanks
If you would like to use regex.
$url = 'https://www.example.com/i/location-name/category/subcategory/item/item-id';
$new_param = 'new-parameter';
print preg_replace('|/location-name/|','/'.$new_param.'/',$url);
I don't recommend search for only location-name (without slash) beacause it will match with for example location-names string.
UPDATE
Based on placement not string you can change that part that way:
$url = 'https://www.example.com/i/location-name/category/subcategory/item/item-id';
$new_param = 'new-parameter';
$new_url = preg_replace('|/i/(.*?)/|','/i/'.$new_param.'/',$url);
print $new_url.'<br/>';
$new_param = 'another-parameter';
$new_url = preg_replace('|/i/(.*?)/|','/i/'.$new_param.'/',$url);
print $new_url.'<br/>';
You will get:
https://www.example.com/i/new-parameter/category/subcategory/item/item-id
https://www.example.com/i/another-parameter/category/subcategory/item/item-id
Alternative solution
If you would like to be sure about change, you can do it another way, something like this:
$url = 'https://www.example.com/i/location-name/category/subcategory/item/item-id';
$new_param = 'new-param';
$parts = parse_url($url);
$path_parts = explode('/',$parts['path']);
$path_parts[2] = $new_param;
$new_path = implode('/',$path_parts);
$new_url = $parts['scheme'].'://'.$parts['host'].$new_path;
print( $new_url);
Try this one:
$new_url = str_replace('location-name', 'my-location', 'https://www.example.com/i/location-name/category/subcategory/item/item-id')

How To Base PHP Include Off of Current Pagename?

I'm currently hardcoding an include into my pages and its a lot of work everytime I want to create a new page. Currently, my url is like this:
http://example.com/folder/one-z-pagename.php?var1=one&var2=two
and in one-z-pagename.php I have an include that looks like this:
include("lander-a-pagename.php");
So what I want to do is instead of hardcoding the file into the page like above, I want to grab one-z-pagename.php without the ?var1=one&var2=two from the url, erase the first 6 characters which is one-z- and replace it with lander-a-.
How do I do something like this?
use parse_url and basename to get filename then str_replace
$url= parse_url('http://example.com/folder/one-z-pagename.php?var1=one&var2=two');
$file = basename($url['path']);
$newfile = str_replace('one-z-','lander-a-',$file);
echo $newfile;
output : lander-a-pagename.php
It can be achive by many method, have a look on below two methods:
Method 1:
$current_script_name = basename(__FILE__);
$include_script_name = 'lander-a-'.substr($current_script_name, 6);
Method 2:
$current_script_name = $_SERVER['SCRIPT_NAME'];
$current_script_name = end(explode('/', $current_script_name));
$include_script_name = 'lander-a-'.substr($current_script_name, 6);
//OR
$include_script_name = str_replace('one-z', 'lander-a', $current_script_name);
variable $include_script_name contains required filename which you want to include.
Hope this will help.

$GET query to directory?

i want to fetch youtube videos from the above script but the above code is getting keyword from GET parameter example.com/s=keyword and i want it to get from a example.com/HERE
i mean you can see there is a $_GET['s']
So this function works like this
example.com/s=keyword
and i want it to work like this
example/page/keyword
sorry for my bad english
$keyword = $_GET['s'];
file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&q=$keyword&type=video&key=abcdefg&maxResults=5");
Have a look at $_SERVER[REQUEST_URI]
This will return you the current url. Then process it using simple string or array functions to get the params, like
$current_url = $_SERVER['REQUEST_URI'];
$url_arr = explode("/", $current_url);
Then access the parameters using the array indexes
like $page = $url_arr[0];

Url Explode for value

Currently I have a url thats like this,
http://website.com/type/value
I am using
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
$array = explode('/',$url);
this to get the value currently but my page has Facebook like's on it and when it is clicked it adds all these extra variables. http://website.com/type/value?fb_action_ids=1234567&fb_action_types= and that breaks that value that I am trying to get. Is there another way to get the specific value?
Assuming you know that this will always be a valid URL, you can use parse_url.
list(, $value) = explode('/', parse_url($url)['path']);
I'd use a preg_replace
explode('/', preg_replace('/?.*$/', '', $url));
You could also use:
$array = explode('/',$_SERVER['PATH_INFO']);
Or, this:
$array = explode('/',$_SERVER['PHP_SELF']);
With this, you do not need the trim() call or the temp var $url - unless you use it from something else.
The reason for two options is I don't know if /type/value is being passed to an index.php or if value is in fact a php file. Either way, one of the two options will give you what you need.

How do i remove a chunk of text from a variable? (PHP)

I need to be able to remove a URL from a variable, I'm wondering how i do this.
Example - Say my script returns http://www.example.com/file.php?id=1234 i need to be able to remove the http://www.example.com/file.php?id= bit, just leaving the id number. If anyone can help, it would be great :)
Something like this?
$var = 'http://www.example.com/file.php?id=1234';
$query = parse_url($var, PHP_URL_QUERY);
$query_components = parse_str($query);
$id = $query_components['id'];
You can use regular expressions:
preg_match("/id=(\\d+)/", $url, $matches);
$id = $matches[1];
Just use $id = $_GET['id'];.
See the docs.
And don't forget to validate and sanitize.
The "id" in this case is being sent to your script as a GET variable, therefore you would access it as follows:
$id = $_GET['id'];
If you mean to say that this URL is not yours to control, then you would do this instead:
print_r(parse_url($url)); // Then analyze the output.

Categories