Why isn't php explode extracting array values

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Why isn't php explode extracting array values



I have some date values (each with a trailing = sign) stored as a &-delimited string.


=


&



My $_POST['dates'] string looks like this:


$_POST['dates']


23.08.18=&22.08.18=&21.08.18=



I'm trying the following PHP code to extract the dates before inserting them into my database table. The code is currently not working.


foreach(explode('&', $_POST['dates']) as $value)

$value1 = explode('=', $value);
foreach ($value1 as $x)




How can I isolate the date values?





Clarification: The problem is that the variables $value1 and $x are empty
– objelland
Aug 9 at 2:37





Possible duplicate of Explode that doesn't return empty strings?
– Yohanes Gultom
Aug 9 at 2:55





What is your exact expected result? How might your input data vary? Might you have duplicate dates?
– mickmackusa
Aug 9 at 2:57





2 Answers
2


<?php

$data = '23.08.18=&22.08.18=&21.08.18=';

//$delimiters has to be array
//$string has to be array

function multiexplode ($delimiters,$string)

$ary = explode($delimiters[0],$string);
array_shift($delimiters);
if($delimiters != NULL)
foreach($ary as $key => $val)
$ary[$key] = multiexplode($delimiters, $val);


return $ary;


$exploded = multiexplode(array("=","&"),$data);

var_dump($exploded);



The result should be:


array (size=3)
0 => string '23.08.18' (length=8)
2 => string '22.08.18' (length=8)
4 => string '21.08.18' (length=8)



We need to use array_filter.


array_filter



http://php.net/manual/en/function.explode.php



[EDIT] mickmackusa's answer is the right tool for parse url parameters into variables.





Your proposal solved my problem, great stuff! After running the multiexplode function I just added a foreach loop to extract the content of the array.
– objelland
Aug 9 at 3:06





@objelland don't multi-explode anything. This answer is an inefficient hack that can be completely replaced with two native function calls.
– mickmackusa
Aug 9 at 3:17



The function that you might be looking for is parse_str(). You have a valid query string (well, kind of) with keys but no values, so you merely need to parse it and isolate the keys(dates). Unfortunately, the parser converts your dots to underscores.


parse_str()



Code: (Demo)


$string = '23.08.18=&22.08.18=&21.08.18=';
parse_str($string, $out);
var_export(array_keys($out));



Output:


array (
0 => '23_08_18',
1 => '22_08_18',
2 => '21_08_18',
)



And of course, you can repair the modified date values with str_replace(). See this demo.


str_replace()



Or if you want to disregard parse_str(), you can use just one regex call that divides the string on = followed by an optional &:


parse_str()


=


&



Code: (Demo)


$string = '23.08.18=&22.08.18=&21.08.18=';
var_export(preg_split('~=&?~', $string, -1, PREG_SPLIT_NO_EMPTY));



Output:


array (
0 => '23.08.18',
1 => '22.08.18',
2 => '21.08.18',
)



Or without regex, just trim the last = and explode on =&: (Demo)


=


=&


$string = '23.08.18=&22.08.18=&21.08.18=';
var_export(explode('=&', rtrim($string, '=')));
// same output as preg_split()





For sure, its the right tool for the job.
– Lawrence Cherone
Aug 9 at 3:07





@objelland I would appreciate if you would re-compare the answers on this page.
– mickmackusa
Aug 10 at 12:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS