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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.