2 回答
![?](http://img1.sycdn.imooc.com/5458643d0001a93c02200220-100-100.jpg)
TA贡献1878条经验 获得超4个赞
替换空格并修剪开头或结尾的逗号和空格:
$result = str_replace(' ', '', trim($string, ', '));
或者:
$result = trim(str_replace(' ', '', $string), ',');
那么如果你只想要数字和逗号(没有字母等)也许:
if(!preg_match('/^[\d,]+$/', $string)) { //error }
然而,这对于没有逗号的单个数字不会出错。
![?](http://img1.sycdn.imooc.com/54586870000183e302200220-100-100.jpg)
TA贡献1824条经验 获得超6个赞
使用
\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 关注
- 110 浏览
添加回答
举报