I need to remove all special characters and spaces on a textfield for a form I'm building. How do I accomplish this in PHP.
A:
Use the following regex during processing of the data:
$data = preg_replace('/[^A-Za-z0-9]/', "", $data);
This will remove all non-alphanumeric characters from the data.
eykanal
2010-06-02 17:09:56
Thank you for the quick reply I will try that now.
Benny
2010-06-02 17:11:13
Missing pattern delimiters in the regexp
kemp
2010-06-02 18:24:15
A:
$specialChars = array(" ", "\r", "\n");
$replaceChars = array("", "", "");
$str = str_replace($specialChars, $replaceChars, $str);
Stijn Leenknegt
2010-06-02 17:10:56
A:
This really depends, I assume you are working with $_POST[] data and wish to sanitize those inputs? If so I would definitely do something like:
$var = preg_replace("/[^A-Za-z0-9]/", "", $var);
That will strip out everything other than alpha/num, you can adjust the regex to include other characters if you wish. Some great examples of commonly used regular expressions can be found at: The RegEx Library
If this isn't quite what you are looking for or have other questions let us know.
Nicholas Kreidberg
2010-06-02 18:11:00
Yes I'm working with $_POST data the exact code I currently have is the following:<input type="text" name="trTrPath" id="trTrPath" value="<?php echo KT_escapeAttribute($row_rstribute['trTrPath']); ?>" size="32" /> <?php echo $tNGs->displayFieldHint("trTrPath");?> <?php echo $tNGs->displayFieldError("tribute", "trTrPath"); ?>How would I add the suggest code to that? Thank you for your help!
Benny
2010-06-02 18:15:44