Reputation: 9
I Have a Variable which contain following elements
$value= ' ["1","7.9","12\/01\/2015"] ';
Now I want to make a array so, that it will contain following values :-
$array[0] = 7.9;
$array[1] = 12/01/2015;
i want PHP Regex which can able to seprate the following element in the above manner. i tryed
$ans = preg_match('/^[0-9,]+$/', $value);
But this will not work. Plz Help what to do. to get the array with the above manner.
array[0] = 7.9; //amount
array[1]= 12/01/2015; //Date
Upvotes: 1
Views: 48
Reputation: 76395
your value " ["1","7.9","12\/01\/2015"] "
seems to me to be a json encoded array, I'd use json_decode
to get what you want:
$value = ' ["1","7.9","12\/01\/2015"] ';
$array = json_decode(trim($value));//remove leading and trailing spaces
//or as pointed out to me in the comments
$array = json_decode($value);
//remove first element ($array is now [1, 7.9, 12/01/2015]
$result = array_slice($array, 1);
var_dump($result);//array(7.9, 12/01/2015)
This is assuming the double quotes around the values (1, 7.9 and the date) are properly escaped, because as it stands, $value
is not a valid string.
Using regex like /[^\/\.,"]+[\.\/][^"\.,]+/
would work, too, but all in all, that's not to be recommended here. Still, for completeness:
preg_match_all('/[^\/\.,"]+[\.\/][^"\.,]+/', $value, $matches);
$result = $matches[0];
var_dump($result);
Upvotes: 3