Jump to content

Explode Nightmare


otonix

Recommended Posts

I am having a nightmare with an explode and cant get it to work. I am posting a string using JQuery to a PHP form which needs to explode the string into an indexed array to be inserted into a database.

 

This is what the string looks like "importance%5B101%5D=50&importance%5B100%5D=50&importance%5B99%5D=50&importance%5B98%5D=50"

 

The original array has been serialised into the above string. The arrayname is importance. The array's index (inside the square brackets) is referenced from an ID from the page.  The square brackets have been replaced by '%5B' and '%5D' and surround the array index by the JQuery. The value for each of these array items is shown as =50. So its basically the same as a GET when we have name=value&name1=value1etc')

 

How would i explode the string above to remove the '%5B' and '%5D' to make the '[' and ']' reappear and then from then it can be read as a GET post? like 'name=value&name1=value1etc'. Using the new array of that index ready to be inserted into a MySQL database.

 

Im going mad trying to solve this!!!  :confused:

Link to comment
https://forums.phpfreaks.com/topic/232191-explode-nightmare/
Share on other sites

This will convert the string into an array (or arrays) based on the name(s) used in the string. In this case it would be an array variable $importance

$encodedString = "importance%5B101%5D=50&importance%5B100%5D=50&importance%5B99%5D=50&importance%5B98%5D=50";

$decodedString = urldecode($encodedString);
$explodedVars = explode('&', $decodedString);
foreach($explodedVars as $nameValuePair)
{
    preg_match("#(.*)\[([^\]]*)\]=(.*)#", $nameValuePair, $matches);
    ${$matches[1]}[$matches[2]] = $matches[3];
}

print_r($importance);

/*Output---
Array
(
    [101] => 50
    [100] => 50
    [99] => 50
    [98] => 50
)
*/

Link to comment
https://forums.phpfreaks.com/topic/232191-explode-nightmare/#findComment-1194467
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.