tags:

views:

56

answers:

3

I'm wanting to use http_build_query to change an array to html tag properties. Problem is, it's changing my single-quoted values into %27. So if I have

http_build_query( array("type"=>"'hidden'", ... ), '', ' ' );

I get

<input type=%27hidden%27 ...>

How can I get around this?

A: 

I guess you could get around this doing a rawurldecode() on the result, but this really isn't what http_build_query was intended for. And won't it put a & between the name/value pairs anyway, making the output unusable as a input element?

You could use one of the XML classes to do this but I'm not sure it's worth the effort. Where are you using this?

Pekka
The third argument is the argument separator for the result string, and I set it to `' '`, no now there are spaces instead of ampersands. Basically I want to do an `implode()`, but also use they keys. My array is `array('key'=>'value', 'key'=>'value', ... )` and I need the keys in there.
A: 

you could add urldecode() in front of the http_build_query

like:

<?php
     urldecode(http_build_query( array("type"=>"'hidden'", ... ), '', ' ' ));
?>
Mihai Iorga
+1  A: 

http_build_query() was designed to turn an array of parameters into a URL. Not to build an HTML tag. You can do a few things:

  1. Add it all manually

    <input type="<?php echo htmlspecialchars($array['type']); ?>" ...
    
  2. Build a helper function

    function buildArgs($array) {
        $ret = '';
        foreach ($array as $key => $value) {
            $ret .= ' ' . htmlspecialchars($key, ENT_QUOTES) . '="' . htmlspecialchars($value) . '"';
        }
        return trim($ret);
    }
    
    
    <input <?php echo buildArgs(array('type'=>'hidden', 'name'=>'foo')); ?>>
    

Would yield you:

    <input type="hidden" name="foo" >
ircmaxell
Why write a new function when `http_build_query()` does almost exactly what I want it to? `urldecode()` makes it work perfectly.
No it doesn't work perfectly. `http_build_query()` will escape arguments as they would need to be to be in a URL (Which you unescape with urldecode). It will do nothing for html escaping. So if you used it, you'd be introducing XSS vulnerabilities. Just because something appears to work, doesn't mean that you should use it...
ircmaxell
Also note that your helper function doesn't scrub keys ( neither does my single line function ), so an xss vulnerability exists there . A false sense of security, indeed! A proper function would also use `htmlspecialchars` on the keys.
Fair point about scrubbing the array keys. I edited that back in. However, I was under the assumption that keys would be generated from within the script, and values added from user input. And this is not a case of reinventing the wheel. It's a case of using functions as they were designed to be used...
ircmaxell
If keys are safe because they are generated within the script, elements can be too. If keys can be scrubbed beforehand, so can elements. If we only used tools according to their original purpose, we'd still be hunting with stone spears. Technological growth comes in part from re-purposing.
Awright, awright, you win -- "Call-time pass-by-reference has been deprecated..." `buildArgs()` it is.