如果我有一个这样的字符串
~1~~2~~3~
如何使用 php 获取数字?
使用正则表达式:
$string = '~1~~2~~3~';
preg_match_all('/~(\w+)~/', $string, $m);
print_r($m[1]);
尝试:
$input = '~1~~2~~3~';
$output = array();
foreach ( explode('~~', $input) as $val ) {
$output[] = (int) trim($val, '~');
}
preg_match_all('/([\d]+)/', $string, $match);
好吧,只是为了让您在阳光下拥有各种变化...... :-)
$s = '~1~~2~~3~';
$a = preg_split('/~+/', $s, -1, PREG_SPLIT_NO_EMPTY);
我会匹配数字:
preg_match_all("/(\d+)/", $string, $numbers)