brown2005 Posted July 7, 2006 Share Posted July 7, 2006 hi i need to check if there are spaces in a string...example if someone enters a username likebrown 2005it will say 1 spaceor if brown2005it will say 0 spacesthanks Quote Link to comment https://forums.phpfreaks.com/topic/13961-checking-if-spaces-in-string/ Share on other sites More sharing options...
Daniel0 Posted July 7, 2006 Share Posted July 7, 2006 I'm not sure, but something like [code]$spaces = preg_match('/ /',$username);[/code] should do it. I haven't tested it though. Quote Link to comment https://forums.phpfreaks.com/topic/13961-checking-if-spaces-in-string/#findComment-54456 Share on other sites More sharing options...
obsidian Posted July 7, 2006 Share Posted July 7, 2006 [quote author=brown2005 link=topic=99798.msg393186#msg393186 date=1152291403]hi i need to check if there are spaces in a string...example if someone enters a username likebrown 2005it will say 1 spaceor if brown2005it will say 0 spacesthanks[/quote]well, daniel0 has a great start, but if you're looking to actually count the spaces, i would recommend something like this:[code]<?php$string = "username 2005";$spaces = preg_replace('|[^ ]|', '||', $string);echo "There are " . strlen($spaces) . " spaces.";?>[/code]good luck Quote Link to comment https://forums.phpfreaks.com/topic/13961-checking-if-spaces-in-string/#findComment-54459 Share on other sites More sharing options...
brown2005 Posted July 7, 2006 Author Share Posted July 7, 2006 hi that actually says 1 when there are not no spaces mate? Quote Link to comment https://forums.phpfreaks.com/topic/13961-checking-if-spaces-in-string/#findComment-54478 Share on other sites More sharing options...
obsidian Posted July 7, 2006 Share Posted July 7, 2006 [quote author=brown2005 link=topic=99798.msg393218#msg393218 date=1152294688]hi that actually says 1 when there are not no spaces mate?[/quote]my bad... you don't need the || inside the second set of quotes :P[code]<?php$string = "username2005";$spaces = preg_replace('|[^ ]|', '', $string);echo "There are " . strlen($spaces) . " spaces.";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13961-checking-if-spaces-in-string/#findComment-54481 Share on other sites More sharing options...
brown2005 Posted July 7, 2006 Author Share Posted July 7, 2006 nope just stays on 0 Quote Link to comment https://forums.phpfreaks.com/topic/13961-checking-if-spaces-in-string/#findComment-54487 Share on other sites More sharing options...
wildteen88 Posted July 7, 2006 Share Posted July 7, 2006 Put spaces in the $string variable first. ege:$string = " username 2005 ";Should say there are 9 spaces Quote Link to comment https://forums.phpfreaks.com/topic/13961-checking-if-spaces-in-string/#findComment-54491 Share on other sites More sharing options...
brown2005 Posted July 7, 2006 Author Share Posted July 7, 2006 thanks it works... nice 1 Quote Link to comment https://forums.phpfreaks.com/topic/13961-checking-if-spaces-in-string/#findComment-54500 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.