Reputation: 25188
I have an array that I pull that looks like this:
[157966745,275000353,43192565,305328212]
...
How do I go about taking that "string" and converting it to a PHP array which I can then manipulate.
Upvotes: 1
Views: 107
Reputation: 5520
The answer by @Felix Kling is much more appropriate given the context of the question.
$new_string = preg_replace('/([\[|\]])/', '', $strarr);
$new_array = explode(',', $new_string);`
Upvotes: 0
Reputation: 1172
With exact that code...
$string='[157966745,275000353,43192565,305328212]';
$newString=str_replace(array('[', ']'), '', $string); // remove the brackets
$createArray=explode(',', $newString); // explode the commas to create an array
print_r($createArray);
Upvotes: 2
Reputation: 338228
$s = "[157966745,275000353,43192565,305328212]";
$matches;
preg_match_all("/\d+/", $s, $matches);
print_r($matches);
Upvotes: 2
Reputation: 816462
This looks like JSON, so you can use json_decode
:
$str = "[157966745,275000353,43192565,305328212]";
$data = json_decode($str);
Upvotes: 10
Reputation: 10485
PHP explode is built just for this.
$result = explode(',', $input)
Upvotes: 0