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 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. 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 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? 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] 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 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 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 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
Archived
This topic is now archived and is closed to further replies.