note 112875 added to function.urlencode
| From: | homebot at yandex dot ru | Date: | Wed, 31 Jul 2013 15:44:26 +0000 |
| Subject: | note 112875 added to function.urlencode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195840@lists.php.net to get a copy of this message | ||
Simple static class for array URL encoding
[code]
<?php
/**
*
* URL Encoding class
* Use : urlencode_array::go() as function
*
*/
class urlencode_array
{
/** Main encoding worker
* @param string $perfix
* @param array $array
* @param string $ret byref Push record to return array
* @param mixed $fe Is first call to function?
*/
private static function encode_part($perfix, $array, &$ret, $fe = false)
{
foreach ( $array as $k => $v )
{
switch ( gettype($v))
{
case 'float' :
case 'integer' :
case 'string' : $ret [ $fe ? $k : $perfix.'['.$k.']' ] = $v;
break;
case 'boolean' : $ret [ $fe ? $k : $perfix.'['.$k.']' ] = ( $v
? '1' : '0' ); break;
case 'null' : $ret [ $fe ? $k : $perfix.'['.$k.']' ] =
'NULL'; break;
case 'object' : $v = (array) $v;
case 'array' : self::encode_part(
$fe?$perfix.$k:$perfix.'['.$k.']' , $v, $ret, false); break;
}
}
}
/** UrlEncode Array
* @param mixed $array Array or stdClass to encode
* @returns string Strings ready for send as 'application/x-www-form-urlencoded'
*/
public static function go($array)
{
$buff = array();
if ( gettype($array) == 'object') $array = (array) $array;
self::encode_part('', $array, $buff, true);
$retn = '';
foreach ( $buff as $k => $v )
$retn .= urlencode($k) . '=' . urlencode($v) . '&';
return $retn;
}
}
#-------------------------------- TEST AREA ------------------------------------
$buffer = array(
'master' =>'master.zenith.lv',
'join'
=>array('slave'=>'slave1.zenith.lv','slave2'=>array('node1.slave2.zenith.lv','slave2.zenith.lv')),
'config' => new stdClass()
);
$buffer['config']->MaxServerLoad = 200;
$buffer['config']->MaxSlaveLoad = 100;
$buffer['config']->DropUserNoWait = true;
$buffer = urlencode_array::go($buffer);
parse_str( $buffer , $data_decoded);
header('Content-Type: text/plain; charset=utf-8');
echo 'Encoded String :' . str_repeat('-', 80) . "\n";
echo $buffer;
echo str_repeat("\n", 3) . 'Decoded String byPhp :' . str_repeat('-',
80) . "\n";
print_r($data_decoded);
[/code]
----
Server IP: 69.147.83.199
Probable Submitter: 83.99.225.247
----
Manual Page -- http://php.net/manual/en/function.urlencode.php
Edit -- https://master.php.net/note/edit/112875
Del: integrated -- https://master.php.net/note/delete/112875/integrated
Del: useless -- https://master.php.net/note/delete/112875/useless
Del: bad code -- https://master.php.net/note/delete/112875/bad+code
Del: spam -- https://master.php.net/note/delete/112875/spam
Del: non-english -- https://master.php.net/note/delete/112875/non-english
Del: in docs -- https://master.php.net/note/delete/112875/in+docs
Del: other reasons-- https://master.php.net/note/delete/112875
Reject -- https://master.php.net/note/reject/112875
Search -- https://master.php.net/manage/user-notes.php