how to turn ?page=home to ?home and still use $_GET - php

Is there anyway to turn index.php?page=home to just index.php?home, so I can still use $_GET? or is this impossible? I would have googled this but I couldn't find anything anywhere.
UPDATE let me be more clear, i need to get the string AFTER index.php? so i can use it in my php code

If you want to use the URL and string the hostname out you can use Robert Elwell answer
$foo = "http://www.example.com/foo/bar?hat=bowler&accessory=cane";
$blah = parse_url($foo);
print_r($blah);
Array
(
[scheme] => http
[host] => www.example.com
[path] => /foo/bar
[query] => hat=bowler&accessory=cane
)
If you want to use any string you could use [substr][2] to return only a part of a string.
If you have a more concrete example I could give an example on how to solve it.

Related

Show array as written with rand inside

I have an array such as:
$var = array('hi','ho',rand(2,5));
What I would like to echo is the entire array, exactly as written.
Normally when you try a print_r, it shows as:
Array (
[0] => hi
[1] => ho
[2] => 3
)
But I want:
Array (
[0] => hi
[1] => ho
[2] => rand(2,5)
)
You can get this with file_get_contents, but is there any way to do so within the actual PHP file?
I don't think it's possible because when array is created, random value is assigned to element with index 2 and you cannot check how this value was created.
I don't think it's possible, since the rand is already evaluated as soon as you set the array to some variable.
A workaround would be the hold the expression as a string and then eval it when you need it. Like this:
$varStr = "array('hi','ho',rand(2,5))";
echo $varStr;
// when you actually need it
$var = eval($varStr);
However, this is almost never a good idea. Providing a use-case where you need this might help come up with a better solution.

escape 'value' in OO PHP

I was not sure how to title this question feel free to re-word it...
I am dealing with a SOAP response that has an object by the name of 'value'(I know... not smart it is third party so I cannot change it!)
$application = $result->return[$i]->extendedist[12]->value->displayValue;
[value] => stdClass Object
(
[displayValue] =>
[internal] =>
[id] =>
)
As you can see this is a problem because PHP wants to use its built in value function. How can I escape this and use the value object from the response?
$result->{'return'}
Should work.
Depending on how you read the response you could decode it into an array instead and use
$result['return']
You can access an object property such as $foo->value as $foo->{'value'} as well.

Parsing out "site.com" from a passed variable in PHP?

I'm passing through a variety of URLs in a global variable called target_passthrough, so the URL of a page might look like:
http://www.mysite.com/index.php?target_passthrough=example.com
Or something like that. Formats for that variable may be a variety of things such as (minus quotes):
"www.example.com"
".example.com"
"example.com"
"http://www.example.com"
".example.com/subdir/"
".example.com/subdir/page.php"
"example.com/subdir/page.php"
Please note how some of those have periods as the first character such as 2,5, and 6.
Now, what I am trying to do is pull out just "example.com" from any of those possible scenarios with PHP and store it to a variable to echo out later. I tried parse_url but it gives me the "www" when that is present, which I do not want. In instances where the url is just "example.com" it returns a null value.
I don't really know how to do regex matching or if that is even what I need so any guidance would be appreciated--not really that advanced at php.
As you pointed out, you can use parse_url to do much of the work for you and then simply strip off the www or leading dot if it is present.
An alternative strategy of taking the last two "words" won't always work because there are domains like www.example.co.uk. Using this strategy would give you co.uk instead of example.co.uk. There is no simple rule for determining which parts are the domain or the sub-domain.
parse_url() outputs an array the different parts of the URL. You are getting null values because you are only referencing the first item in the array. parse_url()
Array (
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)

Array call from key not working correctly

I'm trying to call to a specific part of an array with the key # and it's not working. I can output the array and see it...
Array
(
[6] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[7] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[8] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
)
This array is $emailDB. I can call to the array manually with $emailDB[7] and it works, but if my call is dynamic like this it won't work...
<?php
$value = 7;
print_r($emailDB[$value]);
?>
I've never had an issue like this with an array so it's very odd. What really sucks is I'm under deadline with a form not working on a client's site...joy.
We tried this with no luck...
<?php
$value = 7;
print_r($emailDB[intval($value)]);
?>
I thought intval() would assist but it did not.
You're post implies a bug in php itself, which I highly doubt. What's more likely is that what you posted doesn't properly represent the code you're running.
Why don't try this. Make a brand new empty php file. Hardcode the array keys and values and assign them to the $emailDB variable, and then try
$value = 7;
print_r($emailDB[$value]);
You will see you don't have the problem that you claim. You have now started the debugging process, and now you can look at the working, and non working code to compare the difference.
Well, you are echoing an Array, which I assume is printing "Array" onto your screen. If you want to echo the actual contents of the array, you need to use print_r($array) or echo print_r($array, true). You can also try putting the value in quotes, like $emailDB["{$value}"] to see if that works, I sometimes have troubles with integers not going into things properly.
I agree with you all. It had to have been something whacky with how we were pulling in the data somehow. It was a tab-separated file we were exploding. I just re-wrote the whole thing entirely and imported the data into MySQL and all was well.
In hindsight, I have a sneaking suspicion it was a trim() command that was needed and likely nothing more. Dang it...too late, but I learned something about checking over the code for those types of things.

How to pass querystring to testAction in CakePHP 1.2?

In CakePHP putting a querystring in the url doesn't cause it to be automatically parsed and split like it normally is when the controller is directly invoked.
For example:
$this->testAction('/testing/post?company=utCompany', array('return' => 'vars')) ;
will result in:
[url] => /testing/post?company=utCompany
While invoking the url directly via the web browser results in:
[url] => Array
(
[url] => testing/post
[company] => utCompany
)
Without editing the CakePHP source, is there some way to have the querystring split when running unit tests?
I have what is either a hack (i.e. may not work for future CakePHP releases) or an undocumented feature.
If the second testAction parameter includes an named array called 'url' then the values will be placed in the $this->params object in the controller. This gives us the same net result as when the controller is directly invoked.
$data = array ('company' => 'utCompany') ;
$result = $this->testAction('/testing/post', array
(
'return' => 'vars',
'method' => 'get',
'url' => $data)
) ;
I'm satisfied with this method for what I need to do. I'll open the question to the community shortly so that it in the future a better answer can be provided.
None of these answers will woerk in Cake 1.3. You should instead set the following before your testAction call:
$this->__savedGetData['company'] = 'utcompany';
CakePHP does provide some level of url splitting but it only seems to work in the run-time configuration and not the test configuration. I'll contact the CakePHP if this is intentional.
I suggestion for your querystring parser would be to use the PHP function explode.
I believe you can do something like this:
$result = explode ('&', $queryString, -1) ;
which would give you your key-pairs in seperate array slots upon which you can iterate and perform a second explode like so:
$keyPair = explode ('=', $result[n], -1) ;
However, all this being said it would be better to peek under the hood of CakePHP and see what they are doing.
What I typed above won't correctly handle situations where your querystring contains html escaped characters (prefixed with &), nor will it handle hex encoded url strings.
use _GET['parmname'];

Categories