membot Posted October 27, 2010 Share Posted October 27, 2010 I want to be able to keep people from entering certain characters in a form. I've tried google, and had no luck so far. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/ Share on other sites More sharing options...
kenrbnsn Posted October 27, 2010 Share Posted October 27, 2010 You can use Javascript to check the characters while the user is entering them. You can use PHP to check the data once the form has been submitted. Ken Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127180 Share on other sites More sharing options...
membot Posted October 27, 2010 Author Share Posted October 27, 2010 I'd like to use PHP, since that's what I'm learning. Could you lead me to a certain function? Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127190 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2010 Share Posted October 27, 2010 You can strip them out with str_replace(). Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127320 Share on other sites More sharing options...
Anti-Moronic Posted October 27, 2010 Share Posted October 27, 2010 str_replace() won't suffice if you want to strip out many different 'types' of characters. You can use preg_replace for that but you need to learn some basic regex. Here is what you are looking for: http://www.phpro.org/tutorials/Validating-User-Input.html this will strip out: (a,b,c,d,e): $str = 'abq dadewcdefgh'; echo preg_replace("/[abcde]/", "", $str); Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127324 Share on other sites More sharing options...
Pikachu2000 Posted October 28, 2010 Share Posted October 28, 2010 For the example you provided, str_replace() would be fine. There's no reason to use a regex when a string function will do the job. Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127369 Share on other sites More sharing options...
Anti-Moronic Posted October 28, 2010 Share Posted October 28, 2010 For the example you provided, str_replace() would be fine. There's no reason to use a regex when a string function will do the job. Didn't realize - so do you mean use a series of str_replace to strip out each character? When would that become inefficient? How many characters say on a sample str size of 1000 characters? Seems I need to delve more into the performance hit. Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127376 Share on other sites More sharing options...
Pikachu2000 Posted October 28, 2010 Share Posted October 28, 2010 Not quite sure what you mean by 'use a series of str_replace', but this will perform the same task as the preg_replace above. Performance-wise, based on the average of 100,000 iterations, they're nearly identical. $str = 'abq dadewcdefgh'; $repl = range( 'a', 'e'); echo str_replace($repl, '', $str); Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127384 Share on other sites More sharing options...
Anti-Moronic Posted October 28, 2010 Share Posted October 28, 2010 Not quite sure what you mean by 'use a series of str_replace', but this will perform the same task as the preg_replace above. Performance-wise, based on the average of 100,000 iterations, they're nearly identical. $str = 'abq dadewcdefgh'; $repl = range( 'a', 'e'); echo str_replace($repl, '', $str); Ahh, I see. Thanks! Very helpful. Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127403 Share on other sites More sharing options...
zane23snider Posted October 28, 2010 Share Posted October 28, 2010 As per my view PHP: is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages.So can you tell me the best topic for my project which is very easy to develop and design. Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127483 Share on other sites More sharing options...
membot Posted October 28, 2010 Author Share Posted October 28, 2010 str_replace() will work great for me, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/217024-how-can-i-not-allow-certain-characters/#findComment-1127646 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.