你好我怎樣才能讓我只允許數字[^ 0-9]和數字前面的減號。例如: 有效值= -1 ...- 9,無效= --1 -...在preg_replace幫助申請例外
回答
只是刪除所有的無效字符,並檢查其餘有一個有效的格式:
$cleaned = preg_replace('/[^-0-9]+/', '', $str);
if (preg_match('/^-?[0-9]+$/', $cleaned)) {
// now valid
}
好吧,這裏是另一個建議:
preg_replace('/.*?(-?\d+).*/', '$1', $str)
謝謝,但是當我應用它時,它什麼也不返回: var_dump(preg_replace('/(^ - ?[0-9 ] +)?。* |^[^ - 0-9] + /','$ 1',' - $ ---- matches-- 12--')); //輸出字符串''(長度= 0) – jartaud 2010-07-29 19:14:23
@jartaud:我撤回了我的第一個建議。 – Gumbo 2010-07-29 19:28:46
全景圖是:Im爲_GET變量掃描,幫助我使用JS go方法。現在我想要做的是刪除除減號以外的所有字符?get = - 2'''#$%$%$應該是:?get = -2 – jartaud 2010-07-29 19:29:07
應儘可能簡單...
preg_match('#^-?[0-9]$#', $input);
更新
preg_replace('#-\d#', $replacement, $input);
注意問號使得減號是可選的,如果需要刪除問號。 – 2010-07-29 18:38:51
感謝您的幫助,是的,標誌是必需的,我只需要在前面(-digit) – jartaud 2010-07-29 18:41:43
我添加了基於新信息的更新。 – 2010-07-29 18:58:33
/^-\d+$/
,或者如果減去是可選
/^-?\d+$/
- 1. 幫助,NullReference例外,在C#
- 2. 幫助,在Hibernate例外
- 3. php貸款申請設計幫助
- 4. 規劃提出申請需要幫助
- 5. 幫助的了preg_replace
- 6. 幫助用了preg_replace
- 7. PHP的preg_replace幫助
- 8. 休眠例外幫助:TransientObjectException
- 9. PHP Preg_replace需要幫助
- 10. php preg_replace需要幫助
- 11. 需要用的preg_replace幫助
- 12. 財產申報的幫助
- 13. 幫助preg_replace,停止在非alpa_numeric字符
- 14. linq udpate:「找不到或更改的行」。例外。請幫助!
- 15. 需要一些快速幫助與Preg_Replace :)請:)
- 16. 關於Axis2部署的幫助例外
- 17. 蟒蛇嘗試/例外幫助
- 18. 文件上傳thows例外 - 幫助
- 19. tr:nth-child(偶數)幫助。如何申請一個班級?
- 20. DropDownListFor()幫助請
- 21. 正則表達式幫助(PHP,preg_replace)
- 22. PHP需要preg_replace表達式幫助
- 23. 幫助我瞭解PHP的preg_replace函數
- 24. 幫助的preg_replace和特殊字符
- 25. 的preg_replace需要幫助的表達
- 26. Javascript對象申報的幫助
- 27. 一點點幫助請幫助
- 28. ArrayIndexOutOfBoundsException請幫助我
- 29. 請收藏幫助
- 30. 年度申請Excel申請
[^ 0-9]匹配「*不*一個數字,從0-9' 。 – 2010-07-29 18:40:09