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. Link to comment https://forums.phpfreaks.com/topic/214859-validating-user-input/ 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. Link to comment https://forums.phpfreaks.com/topic/214859-validating-user-input/#findComment-1117754 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.'; } Link to comment https://forums.phpfreaks.com/topic/214859-validating-user-input/#findComment-1117755 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. Link to comment https://forums.phpfreaks.com/topic/214859-validating-user-input/#findComment-1117878 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/ Link to comment https://forums.phpfreaks.com/topic/214859-validating-user-input/#findComment-1117952 Share on other sites More sharing options...
DCM Posted October 1, 2010 Author Share Posted October 1, 2010 Thats great thanks a lot. Link to comment https://forums.phpfreaks.com/topic/214859-validating-user-input/#findComment-1118154 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.