Jump to content

Archived

This topic is now archived and is closed to further replies.

brown2005

checking if spaces in string

Recommended Posts

hi i need to check if there are spaces in a string...

example if someone enters a username like

brown 2005

it will say 1 space

or if

brown2005

it will say 0 spaces

thanks

Share this post


Link to post
Share on other sites
I'm not sure, but something like [code]$spaces = preg_match('/ /',$username);[/code] should do it. I haven't tested it though.

Share this post


Link to post
Share on other sites
[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 like

brown 2005

it will say 1 space

or if

brown2005

it will say 0 spaces

thanks
[/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

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
Put spaces in the $string variable first. ege:
$string = "  username  2005  ";
Should say there are 9 spaces

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.