1. 程式人生 > >如何在一長字串中提取出所有數字?

如何在一長字串中提取出所有數字?

1.定義正則
$ex = "/\d+/";

2.檢測並存入陣列

$arr = [];
txt = '';
preg_match_all($ex,$txt,$arr);





例:
txt = 'TC-比特幣>BTC-比特幣 </a><td class=market-cap data-usd=116666294488 data-cny=807815528757 data-btc=17324675>¥8,078億<td><a href=/currencies/bitcoin/#markets target=_blank class=price data-usd=6734 data-cny=46628 data-btc=1>¥46,628</a><td>1,732萬<td><a href=';

提取出的數字:
array (
  0 => 
  array (
    0 => '116666294488',
    1 => '807815528757',
    2 => '17324675',
    3 => '8',
    4 => '078',
    5 => '6734',
    6 => '46628',
    7 => '1',
    8 => '46',
    9 => '628',
    10 => '1',
    11 => '732',
  ),
)