chibineku Posted August 23, 2009 Share Posted August 23, 2009 I have a JavaScript function, well three versions of the same one, that do live pattern matching to prevent users typing in illegal characters in form fields. But, with JavaScript disabled, this doesn't work. I want to implement PHP regex pattern matching on strings before I commit them to my database (in fact, before I even use the input to decide if require fields are filled in properly). I want to strip any characters that are not legal and leave the rest of the string in tact.The patterns I want to match are:One for address, which in JavaScript is: function checkChars(el) { el.value = el.value.replace(/[^a-zA-Z0-9 -]/g, "");}One for usernames, which in JavaScript is:function checkCharsUsername(username) { username.value = username.value.replace(/[^a-zA-Z0-9_.-]/g, "");}And one for e-mail addresses, which in JavaScript is:function checkCharsEmail(email) { email.value = email.value.replace(/[^a-zA-Z0-9@_.-]/g, "");}I have consulted my PHP book, but it is less than clear, same goes for the php.net manual. I thought eregi_replace() was what I wanted, but it has been deprecated and will eventually be removed.Any help, as always, is much appreciated. Link to comment Share on other sites More sharing options...
Synook Posted August 23, 2009 Share Posted August 23, 2009 Just use preg_replace(). EREG (Extended REGular expressions) has (rightly) been dropped in favor of PREG (Perl-compatible REGular expressions). function check_chars($el) {return preg_replace("/[^a-zA-Z0-9 -]/", "", $el);}//etc. Link to comment Share on other sites More sharing options...
chibineku Posted August 23, 2009 Author Share Posted August 23, 2009 Awesome, thanks It's so simple when you see it, but I am so unsure of how to get started. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.