Question

I have a question variable e.g. "4X9"

how can i split this into 3 different variables, to have an integer 4, integer 9 and string X ?

I tried using

$arr = explode('X', $question);
    $before = $arr[0];

    $bbefore = str_replace('"', "", $before);
  $newBefore =(int)$before;`

and the same for after.

Était-ce utile?

La solution

list($before, $x, $after) = str_split(str_replace('"', '', $question));

1) Explode string by character using str_split()
2) Use list() to make assigning them variables clearer
3) If $before and/or $after are integers you can cast them after this line of code

list($before, $x, $after) = str_split(str_replace('"', '', $question));   
$before = (int) $before;   
$after  = (int) $after;   

Demo

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top