Love2c0de Posted September 18, 2013 Share Posted September 18, 2013 Good mornng all, I have a simple contact form which ask for some details from the user including name, number, email, referrer and any comments they may have: <form method="post" action="?page=Contact"> <fieldset> <legend>TSPV-Websites</legend> <p><label for="name">Name:</label><input type="text" id="name" name="name" value="<?php if(isset($_POST['name'])){ print(htmlspecialchars($_POST['name'])); } ?>" /> <span>* Required</span></p> <p><label for="phone">Phone:</label><input type="text" id="phone" name="phone" value="<?php if(isset($_POST['phone'])){ print(htmlspecialchars($_POST['phone'])); } ?>" /> <span>* Required</span></p> <p><label for="email">Email:</label><input type="text" id="email" name="email" value="<?php if(isset($_POST['email'])){ print(htmlspecialchars($_POST['email'])); } ?>" /></p> <p><label for="referrer">Referrer:</label> <select name="referrer" id="referral_list"> <option name="default">Choose an option...</option> <option name="search_engine">Search Engine</option> <option name="facebook">Facebook</option> <option name="twitter">Twitter</option> <option name="friend">Friend</option> <option name="other">Other</option> </select> </p> <p><label class="last_label" for="comments">Comments:</label><textarea name="comments" rows="7" cols="35"><?php if(isset($_POST['comments'])){ print($_POST['comments']); } ?></textarea></p> <p class="form_btns"><input type="submit" value="Send" /><input type="reset" value="Clear" /></p> </fieldset> </form> I want to convert the phone number into an integer but keep losing the preceeding 0's and I've just entered a mobile number example: 07973 762 960 and after I get redirected back to the form (because of an invalid email), it shows only: 7973. Not sure what to do. My database field is set to string for a phone number but I want to change it to int. I want to perform some checks on the phone number to make sure it contains 10 or 11 digits and starts with a 0. I realize there are string functions to check first characters of a string and length but need it to be an integer once sent to database and this issue will arise then. My form processing code is: //Process the contact form if(isset($_POST['name'])) { foreach($_POST as &$v) { $v = trim($v); } if($_POST['name'] == "" || $_POST['phone'] == "") { $error = "<span class='error'>Please fill in both required (*) fields.</span>"; } $_POST['phone'] = str_replace(array("(",")","-"),"",$_POST['phone']); $len = strlen($_POST['phone']); var_dump($len); $_POST['phone'] = (int) $_POST['phone']; if(!ctype_digit($_POST['phone'])) { if($len < 10 || $len > 11) { if(isset($error)) { $error = str_replace("</span>"," ",$error); $error .= "<br />Please enter a valid number</span>"; } else { $error = "<span class='error'>Please enter a valid number.</span>"; } } } if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { if(isset($error)) { $error = str_replace("</span>"," ",$error); $error .= "<br />Please enter a valid email</span>"; } else { $error = "<span class='error'>Please enter a valid email.</span>"; } } if(!isset($error)) { //no errors, we have required data - continue } } Thank you for you time and guidance. Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/282241-phone-number-uk-converting-string-to-integer-retain-digits/ Share on other sites More sharing options...
requinix Posted September 18, 2013 Share Posted September 18, 2013 My database field is set to string for a phone number but I want to change it to int.Don't. Just because a value consists of digits does not mean the value is a number. It should be a string, and preferably without formatting applied to it. Quote Link to comment https://forums.phpfreaks.com/topic/282241-phone-number-uk-converting-string-to-integer-retain-digits/#findComment-1450029 Share on other sites More sharing options...
Love2c0de Posted September 18, 2013 Author Share Posted September 18, 2013 Hi requinix, Thanks for the rapid response. I carried on after making the post and came up with this: $digits = array('0','1','2','3','4','5','6','7','8','9'); $len = strlen($_POST['phone']); $phone = &$_POST['phone']; for($x = 0; $x < $len; $x++) { $char = $phone[$x]; if(!in_array($char, $digits)) { $phone = str_replace($phone[$x], "", $phone); } //combats offset issue due to replacing to an empty string shortens length of string. $len = strlen($phone); } var_dump($_POST['phone']); It returns the string containing just numbers, which is really what I want, just numbers. Are you saying I should just leave it as it is, as they input it? Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/282241-phone-number-uk-converting-string-to-integer-retain-digits/#findComment-1450037 Share on other sites More sharing options...
Solution PaulRyan Posted September 18, 2013 Solution Share Posted September 18, 2013 (edited) Why don't you just use preg_replace? Example: <?PHP $number = '<script>07123456789</script>'; $number = preg_replace('/[^0-9]/', '', $number); echo $number; ?> Edited September 18, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/282241-phone-number-uk-converting-string-to-integer-retain-digits/#findComment-1450043 Share on other sites More sharing options...
Love2c0de Posted September 18, 2013 Author Share Posted September 18, 2013 Much better solution to what I had, thanks for the input!. Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/282241-phone-number-uk-converting-string-to-integer-retain-digits/#findComment-1450055 Share on other sites More sharing options...
requinix Posted September 18, 2013 Share Posted September 18, 2013 preg_replace() to sanitize, ctype_digit to validate. Side note: $phone = &$_POST['phone'];It's bad practice to change the contents of $_GET and $_POST in case some code somewhere really did want the original values. Quote Link to comment https://forums.phpfreaks.com/topic/282241-phone-number-uk-converting-string-to-integer-retain-digits/#findComment-1450098 Share on other sites More sharing options...
Love2c0de Posted September 18, 2013 Author Share Posted September 18, 2013 Your answer would be to store the $_POST['phone'] value into a variable and use that variable for the remainder of the script? Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/282241-phone-number-uk-converting-string-to-integer-retain-digits/#findComment-1450101 Share on other sites More sharing options...
requinix Posted September 18, 2013 Share Posted September 18, 2013 Yup. Quote Link to comment https://forums.phpfreaks.com/topic/282241-phone-number-uk-converting-string-to-integer-retain-digits/#findComment-1450126 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.