Click to See Complete Forum and Search --> : how to trim a string
DARTHTAMPON
10-27-2005, 01:49 AM
ok i have many strings that look something like this
/grid/thegrid.php?cords=0,0,n,0,0,w&pageflag=grid
I was just looking to figure out how to eliminate everything but the 0,0,n,0,0,w from the string.
I was looking at the trim() fuction but it seemed not to do what I wanted it to or I just didnt understand the documentation.
the 0,0,n,0,0,w value will change but will always be in a number comma number comma letter comma number comma number comma letter format.
any ideas?
I also have an issue with sending variable via get.
if i have
"/findbase/index.php?pageflag=fortlvl&fort=2" and need to send it through a form using the get method how do i keep the browser from getting confused and thinking that "pageflag=fortlvl" is part of a variable but not a variable itself?
bokeh
10-27-2005, 03:03 AM
Maybe the following will give you a couple of ideas:<?php
// here's your string
$string = '/grid/thegrid.php?cords=0,0,n,0,0,w&pageflag=grid';
// Make it url safe for use in form with get method
// pretend it is coming from a form
$_GET['safe_string'] = urlencode($string);
// Later, in step two, after form submission extact 'cords'
preg_match('¥cords=(.*?)&¥i', urldecode($_GET['safe_string']), $matches);
$cords = (isset($matches['1'])) ? $matches['1'] : FALSE;
// output
print $cords;
?>
insane
10-27-2005, 06:32 AM
why can't you just use $cords = $_GET['cords'];?
-Insane
chazzy
10-27-2005, 06:38 AM
darthtampon:
me thinks you are not clear on how the get, post arrays function and in general how HTML forms process. you can use a get. to get the information from it (same with post) you need to use the get and post arrays. they are variables. you can't say that "pageflag=fortlvl" is not a variable, that's how its designed to work. if all the requests on this page include pageflag=fortlvl then maybe you don't need to include it?
bokeh
10-27-2005, 06:41 AM
why can't you just use $cords = $_GET['cords'];?
-InsaneBecause 'cords' is just part of a string. Example:
<input type="text" name="string" value="/grid/thegrid.php?cords=0,0,n,0,0,w&pageflag=grid">
As you can see it is not a variable in its own right its just part of a string. This is why he needs urlencode();. If you careful read the last sentance of the post this will become obvious.
bokeh
10-27-2005, 06:49 AM
you can't say that "pageflag=fortlvl" is not a variableOf course you can if those characters are just part of a string! You are just assuming that because those characters look like a query string they are part of one. What is happening is he is trying to send that whole string as a value; it has nothing to do with variables.
chazzy
10-27-2005, 07:00 AM
bokeh:
he would get the same result if he did:
if(isset($_GET['cords'])){
$string = "cords=".$_GET['cords'];
}
elseif{
//check for others..
}
is there some reference to this type of notation in the php manual, because this seems like an insecure way of handling requests imho.
bokeh
10-27-2005, 08:22 AM
This is a missunderstanding. That is not a request or a variable. Example:
$string = "What time is dinner? Any time after six is good for me."; That string also contains a question mark but it has nothing to do with passing a request. The same as also the case with this post, it is just a string. That is why he is having troulbe passing it by the get method; because it looks like a query string.
Also I don't understand the point you make about a security issue since nothing is being executed. Could you explain?
bokeh
10-27-2005, 08:47 AM
Chazzy, I have just thought of a valid example. I have a web page:
http://costablancatranslations.com?language=english
I want to pass that page to the validator at w3.org
http://validator.w3.org/check
The whole thing looks like this:
http://validator.w3.org/check?uri=http://costablancatranslations.com?language=english
Now look at that. We now have a second query string inside the first query string but the second query string is merely a value (and not a request) so it should be encoded with urlencode(). End result: http://validator.w3.org/check?uri=http%3A%2F%2Fcostablancatranslations.com%2F%3Flanguage%3Denglish
chazzy
10-27-2005, 11:40 AM
bokeh:
I understand his issue now. Sorry about that.
DARTHTAMPON
10-27-2005, 10:07 PM
// Later, in step two, after form submission extact 'cords'
preg_match('¥cords=(.*?)&¥i', urldecode($_GET['safe_string']), $matches);
$cords = (isset($matches['1'])) ? $matches['1'] : FALSE;
Could you please further explain thses lines.
preg_match( #used to find a match using regexp's
'¥ # what does the ¥ stand for? What does it do
cords=(.*?) # does this find every character between "cords=" and ?
# What if the string is at the end of the url and not in the middle?
# Well this not confuse php and throw an error or execute a different path
&¥i', # Again with the ¥ and what exactly does "&¥i'," do
# I assume the i is to ignore case but am not familyer with the rest.
urldecode($_GET['safe_string']),
# Decodes the encoded url
$matches); # the return variable. What are the possable otputs of this function?
$cords = # set variable
(isset($matches['1']))
# I thought isset was a cookie thing. Does this just check
# to see if the array has a value?
? # no idea what this does
$matches['1'] : FALSE;
# not sure what this last line does eather.
any clearification would be greatly appreciated.
tia
NogDog
10-27-2005, 10:25 PM
Another way to skin the cat:
<?php
$test = "/grid/thegrid.php?cords=0,0,n,0,0,w&pageflag=grid";
$url = array_pop(explode("?", $test));
parse_str($url, $values);
echo "<p>Coordinates = {$values['cords']}</p>\n";
?>
bokeh
10-28-2005, 04:21 AM
Another way to skin the catI like that better!
SpectreReturns
10-29-2005, 02:41 PM
I would use parseurl() which would return the query string, which you could then parse again as a query string, and then could work with it from there.