我有这样的多个字符串:
[13]带数字化仪的玻璃(iPhone 5),[106]电池(iPad 4),[192] 2GB DDR3 1067 MHz(内存)
我不知道如何从字符串到数组中简单地提取ID?
答案 0 :(得分:2)
您可以尝试:
$input = '[13] Glass with digitizer (iPhone 5), [106] Battery (iPad 4), [192] 2GB DDR3 1067 MHz (Memory)';
preg_match_all('/\[(\d+)\]/', $input, $matches);
$output = array_map('intval', $matches[1]);
输出:
array (size=3)
0 => int 13
1 => int 106
2 => int 192
答案 1 :(得分:0)
使用preg_match_all然后操纵数组以摆脱[]
$string = "[13] Glass with digitizer (iPhone 5), [106] Battery (iPad 4), [192] 2GB DDR3 1067 MHz (Memory)";
preg_match_all("/\[[0-9]*\]/",
$string,
$out);
array_walk_recursive($out[0], 'cleanSquareBrackets');
print_r($out);
function cleanSquareBrackets(&$element) {
$element = str_replace(array("[", "]"), "", $element);
}
<强>输出:强>
Array ( [0] => Array ( [0] => 13 [1] => 106 [2] => 192 ) )
答案 2 :(得分:-1)
$str = "[13] Glass with digitizer (iPhone 5), [106] Battery (iPad 4), [192] 2GB DDR3 1067 MHz (Memory)";
preg_match_all('!\[\d+\]!', $str, $matches);
print_r($matches);
另外,你可以用一个字符串
来获取它$numbers = implode(',', $matches[0]);