Jump to content


Photo

checking if spaces in string


  • Please log in to reply
7 replies to this topic

#1 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 July 2006 - 04:56 PM

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

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 07 July 2006 - 05:08 PM

I'm not sure, but something like
$spaces = preg_match('/ /',$username);
should do it. I haven't tested it though.

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 July 2006 - 05:10 PM

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


well, daniel0 has a great start, but if you're looking to actually count the spaces, i would recommend something like this:
<?php
$string = "username 2005";
$spaces = preg_replace('|[^ ]|', '||', $string);
echo "There are " . strlen($spaces) . " spaces.";
?>

good luck
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 July 2006 - 05:51 PM

hi that actually says 1 when there are not no spaces mate?

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 July 2006 - 06:00 PM

hi that actually says 1 when there are not no spaces mate?


my bad... you don't need the || inside the second set of quotes :P
<?php
$string = "username2005";
$spaces = preg_replace('|[^ ]|', '', $string);
echo "There are " . strlen($spaces) . " spaces.";
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 July 2006 - 06:15 PM

nope just stays on 0

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 July 2006 - 06:21 PM

Put spaces in the $string variable first. ege:
$string = "  username  2005  ";
Should say there are 9 spaces

#8 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 July 2006 - 06:45 PM

thanks it works... nice 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users