find parameters in url using php - php

I am searching for a way to find a url that contains a parameter to extract a dynamic value
example, anywhere on my website there is a button with a link that contains characters
example.com/register/register-company.html?tx_powermail_pi1[regUid]=78
i need the id from regUid to work with queries, the 78 is not static.
thx for any help.

$str = "http://example.com/somepage?tx_powermail_pi1[regUid]=78";
$uri = parse_url($str);
if($uri["query"]){
#if there is query string only....
parse_str($uri["query"], $test);
var_dump($test);
}
This will work on any url. If you are trying to catch your current url get param you can just use
$_GET['tx_powermail_pi1']['regUid']

Related

Create a php variable from URL without question mark

I need to create a variable in PHP from a URL, which does not have a fully formed query string.
e.g. http://search.domain.com/domain2.com
In this example, the variable needs to be
$website='domain2.com'
Is there a way to convert the entered URL in address bar to my ?website= variable?
An example would be the whois.domaintools service, which allows you to query a whois record from their website using the following url format:
http://whois.domaintools.com/domain.com
This then displays info based on the url you specified.
Can i achieve this using a MOD_Rewrite in the .htaccess, or can i use some PHP function like http_build_query to achieve this? I'm going around in circles and surely missing something obvious!
You can use this code to get your array $urlpart
$link = $_SERVER['REQUEST_URI'];
$urlpart = explode('/',trim(parse_url($link, PHP_URL_PATH), '/'));

Using link data as PHP variable input

I have a link. Eg. abc.com/qwerty. I want to extract the part after / of every input just like examples below and use it just like a PHP GET input value and store it to a variable $page. Essentially, the link abc.com/qwerty should work like abc.com/proc.php?x=qwerty
Typed link Part to be used as PHP GET input
abc.com/cvbx cvbx
abc.com/ghvs ghvx
abc.com/pabc pabc
How can I do this?
You can use: $_SERVER['REQUEST_URI']
$request_uri = $_SERVER['REQUEST_URI']; //returns '/cvbx'
$segments = array_filter(explode('/', $request_uri)); //array_filter to remove empty elements.
You can parse the url as described at http://php.net/manual/en/function.parse-url.php .
If you have urls as described there, you can get the path part with:
$input = substr(parse_url($url, PHP_URL_PATH),1);
substr is to remove the starting /

Pass a URL in a GET for PHP

I need to pass a url using a GET address. To give an example which was I have tried:
http://www.example.com/area/#http://www.example.com/area2/
I've also tried replacing the forward slashes with other characters but that doesn't seem to work. How would you pass a url in a GET?
As I have understood, you should use url_encode() and url_decode().
The function url_encode() lets you create a string that can be used as a link.
You should use it this way:
$link = 'goto.php?link=' . url_encode($_POST['target_site']);
And when you were going to redirect to the user defined site (eg), you can decode the parameter given this way:
$decoded_link = url_decode($_GET['link']);
// Now it's safe to use the given URL (for example I can redirect to there)
header('location: ' . $decoded_link);
Hope it helps.
The # character links to an anchor on the page. The browser will automatically scroll to the element with the id after the point sign. So that's not what you're looking for.
To pass a GET parameter, the syntax would be like this:
http://example.com/area?http://example.com/area2
Then, if you var_dump($_GET), you'll see your URL. But, if you have other fields you also want to pass in your URL, you can use key=value pairs, like so:
http://example.com/area?url=http://example.com/area2&param1=a&param2=b
In this case, your URL will be available in $_GET['url'].

Is it possible to get information from this type of url: website.com/hello/richard

For example, if there is a url like www.website.com/hello/richard, would it be possible to echo hello and 100 separately onto my page.
eg:
hello how are you today richard
You can get the data from $_SERVER['REQUEST_URI'] and then do whatever you like with it.
Yes it would be. Try this:
$myURL = $_SERVER['REQUEST_URI'];
$myTokens = explode('/', $myURL);
echo $myTokens[1] . "blah" . $myTokens[2];
This code gets the current URL into the myURL variable, then it calls a function called explode which turns it into an array based on the position of the '\' character. Then it echos out certain elements of that array. If you play around with output using echo you will soon see for yourself what is going on.
Sure that's possible. You can get URL as a string using $_SERVER['request_uri]. Then you might want to use explode function to firm array of strings where delimiter is /. Then you may parse it. Or you can do this via .htaccess using rewrite rule

what is the easiest way of getting a number from a url?

I am hoping someone can help me, I have created url's like this
/this/is/a/test/index.asp
/this/is/a/test/index-1.asp
/this/is/a/test/index-2.asp
/this/is/a/test/index-3.asp
What is the easiest way to strip the number from the URL?
Instead of using variables like this:
/this/is/a/test/index.asp?no=1
/this/is/a/test/index.asp?no=2
/this/is/a/test/index.asp?no=3
to create variables I am using the number in the URL to dynamically call the content on the page.
If the url is: /this/is/a/test/index-3.asp it should use the 3 and match the content according to it, similar as if I were to call
?no=3
I am using PHP for this...
The url structure will always have the variable define as match the last '-' [the-number] '.asp'
Thanks
Gerald Ferreira
You could use mod_rewrite to map URL patterns to actual URLs. You could achieve what you want with something similar to the following:
RewriteRule ^index-([0-9]+).asp$ index.asp?no=$1
You should be able to match it out with:
if (preg_match('/\-(\d+)\.asp$/', $_SERVER['REQUEST_URI'], $a)) {
$pageNumber = $a[1];
} else {
// failed to match number from URL
}

Categories