我有这样的多个字符串:
13玻璃带数字化器(iPhone 5),106个电池(iPad 4),192 2GB DDR3 1067 MHz (内存)
我不知道如何简单地从字符串提取ID到数组?
发布于 2014-02-03 12:02:18
你可以尝试:
$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发布于 2014-02-03 12:08:31
使用全,然后操作数组以摆脱[]
$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 ) )发布于 2014-02-03 12:04:04
$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]);https://stackoverflow.com/questions/21526845
复制相似问题