指示客戶端僅輸入最優雅的方式來清潔一個字符串轉換爲僅逗號分隔標記
數逗號數逗號數
後(沒有固定的長度,但一般< 10),所述他們的投入結果一直是,呃,不可預測的。
考慮下面的例子中輸入:
3,6 ,bannana,5,,*,
我怎麼能最簡單地,可靠地結束了:
3,6,5
到目前爲止,我試圖組合:
$test= trim($test,","); //Remove any leading or trailing commas
$test= preg_replace('/\s+/', '', $test);; //Remove any whitespace
$test= preg_replace("/[^0-9]/", ",", $test); //Replace any non-number with a comma
但是在我繼續扔東西之前...有沒有一種優雅的方式,可能來自正則表達式!
我認爲,而不是隻是清理輸入,你也應該把輸入驗證使用javascript – 2017-08-25 07:27:07
這是一個非常好的主意,已經滑了我的腦海! – mayersdesign
你也可以讓瀏覽器通過使用輸入類型編號https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input – rypskar