我想通過preg_split函數將字符串轉換爲數組。我想獲得一個包含1個字母和可選數字的數組。對於xample,如果我有 「NH2O3」,我想這樣的輸出:警告:preg_split()[function.preg-split]:編譯失敗:範圍在字符類中的順序不正確
[0] => N,
[1] => H2,
[2] => O3
我有這樣的代碼:
$formula = "NH2O3";
$pattern = '/[a-Z]{1}[0-9]?/';
$formula = preg_split($pattern, $formula);
但這檢索錯誤:
Warning: preg_split() [function.preg-split]: Compilation failed: range out of order in character class at offset 3 in /home/masqueci/public_html/wp-content/themes/Flatnews/functions.php on line 865 bool(false)
這會修正這個錯誤,但不會仍然給出所需的結果。 –
爲了便於閱讀,最好使用命名常量而不是int值。 – Bell
@貝爾:你說得對,但我不想有一個滾動條。 –