Balderick Posted September 3, 2016 Share Posted September 3, 2016 I want to use preg_replace to input only a-zAZ with a limit of 30 characters I have a piece of code I used before but it works with preg_match. example function: <?php // input form to get the name // use the function here $name = valid_name($name); // function to validate name input function valid_name($data){ $data = ltrim($data); $data = rtrim($data); $data = preg_replace("/^(?:[A-Za-z][A-Za-z\-]{2,30}[A-Za-z]|[A-Za-z])$/", '' , $data); return $data; } ?> To give an impression of how I use it; a part of the confirmation form <html> <form action="mypage.php" method="post"> <p><input type="text" name="name" value="<?php echo $name; ?>" STYLE="background-color: #708DCC; color: white; height: 20px; width: 200px; border: none; font-size: 14px;" readonly ></p> <!-- rest of the form --> </html> The problem is in the preg_replace part. an input with chars like: ^&$ etc is not replaced, how to do this? if anyone can help I would be really happy. Link to comment Share on other sites More sharing options...
Ingolme Posted September 3, 2016 Share Posted September 3, 2016 That's not actually validation, that's sanitation. Validation would be if you told the user that the name they posted is invalid (which would probably be a better idea, since they might not like the outcome of the replacement your code is making). Your current preg_replace() call will actually replace an entire valid string with nothing. Here's how you would remove unwanted characters: $name = preg_replace('[^a-zA-Z]', '', $name); This code removes anything that's not a letter. Link to comment Share on other sites More sharing options...
Balderick Posted September 6, 2016 Author Share Posted September 6, 2016 That's not actually validation, that's sanitation. Validation would be if you told the user that the name they posted is invalid (which would probably be a better idea, since they might not like the outcome of the replacement your code is making). Your current preg_replace() call will actually replace an entire valid string with nothing. Here's how you would remove unwanted characters: $name = preg_replace('[^a-zA-Z]', '', $name); This code removes anything that's not a letter. I had to change the code. I added another delimiter, namly the foreslash /. Can the [ ] be considered as a delimiter? This would mean 2 delimiters are required anyway here's my final solution: $name = preg_replace('/[^a-zA-Z]/', '', $name); Link to comment Share on other sites More sharing options...
Ingolme Posted September 6, 2016 Share Posted September 6, 2016 I forgot the delimiters, but [ ] are not delimiters, they are part of the regular expression. If you don't know regular expressions, here's a place you can learn about them: http://www.regular-expressions.info/ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now