rottenpixies Posted June 13, 2007 Share Posted June 13, 2007 Is there any sort of function to be able to go through a string and return if there is a certain character in it. I have a variable for a persons name but and i need to know if there is a space between there name or not. Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/ Share on other sites More sharing options...
quickstopman Posted June 13, 2007 Share Posted June 13, 2007 preg_match() ereg() and eregi() all do that Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/#findComment-274012 Share on other sites More sharing options...
taith Posted June 13, 2007 Share Posted June 13, 2007 if(ereg(' ',$string)) echo 'you have spaces'; else echo 'no spaces!'; Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/#findComment-274016 Share on other sites More sharing options...
trq Posted June 13, 2007 Share Posted June 13, 2007 Best to use strpos for simple searches. Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/#findComment-274017 Share on other sites More sharing options...
wildteen88 Posted June 13, 2007 Share Posted June 13, 2007 use a simple regex pattern to find spaces within a string: $name = 'rotten pixies'; if(preg_match('/[\s]/', $name)) { echo 'Your name contains spaces. This is an invalid character to be used'; } If you want to block other characters, such as underscores (_), pipes (|), astricks (*), etc... Then you can change the the pattern to this: '/[\s_|*]/' add as many characters as you like between the square brackets for characters you wish to disallowed. Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/#findComment-274018 Share on other sites More sharing options...
GingerRobot Posted June 13, 2007 Share Posted June 13, 2007 Thorpe is right here - if you're just checking to see if a character exists in a string, use strpos() - it's faster. Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/#findComment-274025 Share on other sites More sharing options...
neel_basu Posted June 13, 2007 Share Posted June 13, 2007 Ya I agree with thorpe if(strpos($string, 'c')) { //Yes } else { //No } //Assuming that teh Character is c Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/#findComment-274043 Share on other sites More sharing options...
rottenpixies Posted June 13, 2007 Author Share Posted June 13, 2007 I don't know if I was using them incorrectly but the only function that worked was taiths script with the ereg function. if(ereg(' ',$string)) echo 'you have spaces'; else echo 'no spaces!'; Much appreciate the quick response from everyone, you guys are great here!! Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/#findComment-274083 Share on other sites More sharing options...
taith Posted June 16, 2007 Share Posted June 16, 2007 YAY ME! Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/#findComment-275757 Share on other sites More sharing options...
redarrow Posted June 16, 2007 Share Posted June 16, 2007 wildteen metod is best for spaces but other then that for a quick haystack find strpos i say. Quote Link to comment https://forums.phpfreaks.com/topic/55444-detecting-if-a-string-has-a-certain-character/#findComment-275773 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.