DCM Posted September 30, 2010 Share Posted September 30, 2010 Hi, what is they best way to validate user input of strings? A couple of examples would be : 1> If i wanted to check for the existance on the coma ',' character in a string and replace it with a dash '-' character? 2> To check wether a user has entered a valid ip address in the form of x.x.x.x where x can range from 0-255? I think i may need ereg/preg to do this but i have no idea about how to layout the syntax. Thanks for looking. Quote Link to comment Share on other sites More sharing options...
Pawn Posted September 30, 2010 Share Posted September 30, 2010 1) $str = str_replace(",","-",$str) 2) I'm no regex guru, but a quick Google turns up plenty of options. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted September 30, 2010 Share Posted September 30, 2010 $str = str_replace(',', '-', $str); //replace commas with hyphens if (preg_match( "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $ip)) { echo 'Valid IP address'; } else { echo 'Invalid IP address.'; } Quote Link to comment Share on other sites More sharing options...
DCM Posted October 1, 2010 Author Share Posted October 1, 2010 Thanks that works although i am having a real problem interpreting the syntax of that regular expression, need to do some backgroung reading i think on PH and regex. Quote Link to comment Share on other sites More sharing options...
DarkMantis Posted October 1, 2010 Share Posted October 1, 2010 Thanks that works although i am having a real problem interpreting the syntax of that regular expression, need to do some backgroung reading i think on PH and regex. Here is an amazing little cheat sheet to learn from. That's how I learnt. Amongst a few other sites http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/ Quote Link to comment Share on other sites More sharing options...
DCM Posted October 1, 2010 Author Share Posted October 1, 2010 Thats great thanks a lot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.