Guy Smith Guy Smith - 2 months ago 6
PHP Question

extracting numbers from in a string

I need to create an array of integers from strings of text composed of the integers separated by whitespace and plus signs, for example

$string = "1 + 2 + 3 + 4";


is extracted into

$array = ('1' , '2' ,'3' , '4');


This needs to be done in php.

Thank you in advance for any help.

Answer

Thankyou for the question refinment.

<?php

$string = '1 + 2 + 3';
$array = explode(' + ', $string);

for ($a=0;$a<count($array);$a++){
  $array[$a] = (int) $array[$a]; // RECAST STRING TO INTEGER
}

?>