2 回答

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
替換空格并修剪開(kāi)頭或結(jié)尾的逗號(hào)和空格:
$result = str_replace(' ', '', trim($string, ', '));
或者:
$result = trim(str_replace(' ', '', $string), ',');
那么如果你只想要數(shù)字和逗號(hào)(沒(méi)有字母等)也許:
if(!preg_match('/^[\d,]+$/', $string)) { //error }
然而,這對(duì)于沒(méi)有逗號(hào)的單個(gè)數(shù)字不會(huì)出錯(cuò)。

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊
使用
\s+|,+\s*$
查看證明
解釋
NODE EXPLANATION
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
,+ One or more ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
PHP:
preg_replace('/\s+|,+\s*$/', '', $input)
- 2 回答
- 0 關(guān)注
- 154 瀏覽
添加回答
舉報(bào)