문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top