Jump to content

checking if spaces in string


brown2005

Recommended Posts

[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
[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]

Archived

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

×
×
  • Create New...

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.