如何将一个字符串修剪为两个整数和一个字符串(php)


how to trim a string into two Integers and one string (php)?

我有一个问题变量,例如"4X9"

如何将其拆分为3个不同的变量,以获得整数4、整数9和字符串X?

我试过使用

$arr = explode('X', $question);
    $before = $arr[0];
    $bbefore = str_replace('"', "", $before);
  $newBefore =(int)$before;`

之后也是如此。

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

1) 使用str_split()按字符分解字符串
2) 使用list()使为其分配变量更加清晰
3) 如果$before和/或$after是整数,则可以在这行代码之后强制转换它们

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

演示