domi771 domi771 - 3 months ago 6
Javascript Question

PHP or Javascript - Getting values out of a string

I have the following string:

[(33, 165) (1423, 254)]


How can I bring the 4 numbers into an Array? I tried with regex so far.

Result should be an array containing 33,165,1423,254.

I can use a PHP and Javascript solution.

Answer

I don't know for what you are using this and why you require this. But if [(33, 165) (1423, 254)] is a string, below php code can be used to create an array of the numbers in this string.

<?php

$string = "[(33, 165) (1423, 254)]";

$string = str_replace([" ","(","[","]"], "", $string);
$string = str_replace(")", ",", $string);
$array = explode(",", $string);

print_r($array);

OUTPUT

Array ( [0] => 33 [1] => 165 [2] => 1423 [3] => 254)
Comments