2

如果我有一个这样的字符串

~1~~2~~3~

如何使用 php 获取数字?

4

5 回答 5

5

使用正则表达式:

$string = '~1~~2~~3~';
preg_match_all('/~(\w+)~/', $string, $m);
print_r($m[1]);
于 2013-04-15T07:41:32.517 回答
4

尝试:

$input  = '~1~~2~~3~';
$output = array();

foreach ( explode('~~', $input) as $val ) {
  $output[] = (int) trim($val, '~');
}
于 2013-04-15T07:42:20.903 回答
3
preg_match_all('/([\d]+)/', $string, $match);
于 2013-04-15T07:42:02.703 回答
1

好吧,只是为了让您在阳光下拥有各种变化...... :-)

$s = '~1~~2~~3~';
$a = preg_split('/~+/', $s, -1, PREG_SPLIT_NO_EMPTY);

运行代码

于 2013-04-15T09:49:31.087 回答
1

我会匹配数字:

preg_match_all("/(\d+)/", $string, $numbers)
于 2013-04-15T07:42:14.443 回答