Jump to content

Get Variable Value From String Help :)


robindean

Recommended Posts

Ampersands are causing chaos for my pages.

 

If I don't use "&" in a url, my pages won't validate. If I do, php appends only the "amp;" part to each of my variables.

 

So, I've devised a solution which is yet to be complete (I need help).

 

Basically, I'm starting off with ...

 

$query = str_replace('&', '&', $_SERVER['QUERY_STRING']);

 

This grabs the entire query and replaces "&" with simply "&".

 

I need a way to locate specific variable values within this, using a method similar to $_GET.

 

Is there a $_GET method for strings? If not and a function is necessary, please guide me through it.

 

The function call should work in a manner such as ...

 

$myVariable = GetVarValue('Variable_Name');

 

How?

Link to comment
Share on other sites

If I don't use "&" in a url, my pages won't validate

 

Care to elaborate?

 

What are you tring to do. Are you sending a GET query from one page to another? is the ampersand there for splitting your query, part of a string, or a messed up file name...

Link to comment
Share on other sites

If I don't use "&" in a url, my pages won't validate

 

Care to elaborate?

 

What are you tring to do. Are you sending a GET query from one page to another? is the ampersand there for splitting your query, part of a string, or a messed up file name...

 

I think the person is referring to xhtml validation? http://validator.w3.org/

Link to comment
Share on other sites

It's a simple thing, really.

 

HTML validation requires that url requests use "&" in place of "&".

 

So, rather than going through a headache process of switching between the two, I simple grab the entire query string (via php on server side) and convert it for easier use.

 

ACCEPT ... I don't know how to use it!

 

I'm not convinced about explode, as it creates bunch of difficulty with becoming an array.

 

I'd rather go about it by finding the variable we need the value for within the string and grabbing ONLY the value.

 

So, if the string is "display=yes&tomorrow=100&you=helpful" ... and I want to grab the VALUE of tomorrow (100) from what is now a STRING (can't use $_GET anymore) ... how?

Link to comment
Share on other sites

Your URL will not cause a page to not validate due to a & in the query string. & is used to seperate queries sent via the GET superglobal. That way you will not need to split your query string as you'll have access to all your variables via $_GET

Link to comment
Share on other sites

You're on the right track with the validation. However, changing the & should only affect the source code -- & should automatically be converted into & by the browser before it requests the URL. If your $_GET has been affected then you've done something wrong. If & is appearing in the URL request, then it's likely you've double-escaped it.

 

In order to generate the correct HTML you can use either htmlentities() or htmlspecialchars() on any text/data entered into the HTML.

Link to comment
Share on other sites

Okay, well, my exact html (one example) reads as follows ...

 

<div id="curtain" style="width:100%;height:100%;position:fixed;top:0;left:0;z-index:2;background:#5d2e23 url(index.php?display=img&w=1&h=1&c=5d2e23);"></div>

 

... if you look at the url() part for the style attributes ... where exactly am I going wrong with this code?

 

How am I to correct it?

 

I've read about htmlspecialchars() but not htmlentities() and couldn't really grasp what I was supposed to do (or where) with it.

 

Please keep posting so that I might solve this issue :)

Link to comment
Share on other sites

That doesn't seem to have any effect.

 

I believe my problem might be stemming from this part of my code (but I have NO idea where or why) ...

 

$display = $_GET['display'];

 

if ($display == 'img') {

header("Content-type: image/png");

$f = $_GET['f'];

$w = $_GET['w'];

$h = $_GET['h'];

if ($f != null) {

$t = substr($f, 0, -4).'_'.$w.'x'.$h.'.png';

if (file_exists($t)) header('location: '.$t);

else {

list($wo, $ho) = getimagesize($f);

$ip = imagecreatetruecolor($w, $h);

$img = imagecreatefrompng($f);

imagecopyresampled($ip, $img, 0, 0, 0, 0, $w, $h, $wo, $ho);

imagepng($ip, $t, 9);

imagepng($ip, null, 9);

}

} else {

$c = strtolower($_GET['c']);

if ($w == $h) $t = 'bg/bg_'.$c.'.png';

else if ($w < $h) $t = 'bg/bg_hr_'.$c.'.png';

else $t = 'bg/bg_vr_'.$c.'.png';

if (file_exists($t)) header('location: '.$t);

else {

$img = @imagecreate($w, $h);

sscanf($c, "%2x%2x%2x", $r, $g, $b);

$sc = ImageColorAllocate($img, $r, $g, $b);

if ($w != $h) {

$a = imagecolorallocatealpha($img, 0, 0, 0, 127);

if ($w < $h) ImageSetPixel($img, 0, 1, $a);

else if ($w > $h) ImageSetPixel($img, 1, 0, $a);

}

imagepng($img, $t, 9);

imagepng($img, null, 9);

}

}

}

Anything standing out to you?

Link to comment
Share on other sites

htmlspecialchars() encodes any characters reserved for HTML code. htmlentities() does the same, but it also encodes anything it can -- things like Chinese letters and whatnot that aren't ASCII. It's not as important if you're using UTF8. You'll get the best results with validation if you combine UTF8 and htmlspecialchars().

 

First let's work out where the problem is. You can temporarily insert the following at the beginning of that code. Then we'll have a better idea of where everything is going wrong.

 

var_dump($_GET);

die("\n".$_SERVER["QUERY_STRING"]);

Link to comment
Share on other sites

I get the following for http://localhost/index.php?display=img&w=1&h=1&c=5d2e23 ...

 

array(4) { ["display"]=>  string(3) "img" ["w"]=>  string(1) "1" ["h"]=>  string(1) "1" ["c"]=>  string(6) "5d2e23" } display=img&w=1&h=1&c=5d2e23

 

and then when I type & in place of & for the address bar, I get ...

 

array(4) { ["display"]=>  string(3) "img" ["amp;w"]=>  string(1) "1" ["amp;h"]=>  string(1) "1" ["amp;c"]=>  string(6) "5d2e23" } display=img&w=1&h=1&c=5d2e23

Link to comment
Share on other sites

I think I found the problem.

 

It was with my css / javascript file.

 

From external .css and .js ... I'm supposed to go ahead and use plain old "&" in the url right?

 

It's only for plain text / html that & is required?

 

At any rate, the browser isn't showing the url related errors any more (iCab for mac shows when a url isn't encoded correctly).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.