Total brainfart, please help, don't allow spaces.

Ask about a PHP problem here.
Post Reply
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Total brainfart, please help, don't allow spaces.

Post by Temor »

Hello. I forgot the name ( like really, I it's like I never saw it before... ) of the function that helps me set allowed characters for example a username.

What I'm trying to do is disallow spaces, and allow underscores( _ ) in the middle of the username, but not at the start or end.

Please help me :P

/Edit: Posting my code is useless since its only an input field and an if statement to check if its been set.
Torniquet
Posts: 52
Joined: Sun Jun 19, 2011 8:10 am
Contact:

Re: Total brainfart, please help, don't allow spaces.

Post by Torniquet »

$username = preg_replace('#[^A-Za-z0-9_-]#', '', $_POST['username']);
Thats what i use. removes any characters that are not of the above. not sure how to do the second bit tho, sorry :(
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Total brainfart, please help, don't allow spaces.

Post by Temor »

Torniquet wrote:
$username = preg_replace('#[^A-Za-z0-9_-]#', '', $_POST['username']);
Thats what i use. removes any characters that are not of the above. not sure how to do the second bit tho, sorry :(
Well, that's a start at least.
Now I got the name of the function. Php.net here I come :)

Thank you.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Total brainfart, please help, don't allow spaces.

Post by jacek »

preg_replace will replace any thing it matches, preg_match will tell you if a match happened.
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Total brainfart, please help, don't allow spaces.

Post by Temor »

jacek wrote:preg_replace will replace any thing it matches, preg_match will tell you if a match happened.
I tried preg_match. But reading php.net didn't help me understand it.



It requires 3 entries, but in php.net they use 4 or 5. And I can't for the life of me figure out what they are for. Please enlighten me :P
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Total brainfart, please help, don't allow spaces.

Post by jacek »

The 4th parameter is an array to put the matches in (you can ignore this) the 5th one controls the structure of the array that you get, as you ignore the 4th one you can also ignore this ;)

The function will return the number of matches, but the preg_match function will stop after the first match. This means that the return value is either 1 or 0 (which can be treated as true or false on if a match has happened.

Do you have any specific questions, or should I keep rambling ? :lol:
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Total brainfart, please help, don't allow spaces.

Post by Temor »

jacek wrote:The 4th parameter is an array to put the matches in (you can ignore this) the 5th one controls the structure of the array that you get, as you ignore the 4th one you can also ignore this ;)

The function will return the number of matches, but the preg_match function will stop after the first match. This means that the return value is either 1 or 0 (which can be treated as true or false on if a match has happened.

Do you have any specific questions, or should I keep rambling ? :lol:
I can't seem to figure out what I'm supposed to put in it, and in what order.
  if(preg_match($pattern, $subject, $matches) === true){
 $errors[] = 'Space Found.';

}
What am I supposed to put in $pattern,$subject and $matches? :S
Torniquet
Posts: 52
Joined: Sun Jun 19, 2011 8:10 am
Contact:

Re: Total brainfart, please help, don't allow spaces.

Post by Torniquet »

if your looking for a space character. i think you need to use \s
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Total brainfart, please help, don't allow spaces.

Post by jacek »

  if(preg_match($pattern, $subject, $matches) === true){
Should be
  if(preg_match($pattern, $subject, $matches) === 1){
because the function returns the number of matches, not true or false.

For $pattern you give a regular expression that you want to match, so for any spaces
  if(preg_match('/^\s+$/', $subject, $matches) === 1){
$subject is just the string you want to check in, so this would be your messages body or usename or what ever.
$matches is the thing I said you don't need ;)
if(preg_match('/^\s+$/', $subject) === 1){
    // $subject contains at least one space.
}
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Total brainfart, please help, don't allow spaces.

Post by Temor »

jacek wrote:
  if(preg_match($pattern, $subject, $matches) === true){
Should be
  if(preg_match($pattern, $subject, $matches) === 1){
because the function returns the number of matches, not true or false.

For $pattern you give a regular expression that you want to match, so for any spaces
  if(preg_match('/^\s+$/', $subject, $matches) === 1){
$subject is just the string you want to check in, so this would be your messages body or usename or what ever.
$matches is the thing I said you don't need ;)
if(preg_match('/^\s+$/', $subject) === 1){
    // $subject contains at least one space.
}

Alright, I think I got it now, thanks Jacek!

One problem tho.. It only returns 1 if the username consists of only spaces. As soon as I add a real character it returns 0.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Total brainfart, please help, don't allow spaces.

Post by jacek »

Try

[syntax]/\s+/[/syntax]
as the pattern.
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Total brainfart, please help, don't allow spaces.

Post by Temor »

jacek wrote:Try

[syntax]/\s+/[/syntax]
as the pattern.
That did it.
Jacek, you're the best! Thank you :)
User avatar
Dylan
Posts: 150
Joined: Fri May 06, 2011 7:14 pm

Re: Total brainfart, please help, don't allow spaces.

Post by Dylan »

I realize you have stated your problem being solved or whatever, however I still feel it is worth mentioning.

You said you wanted to disallow the characters (_) at the beginning and end of the word;
assuming you are just wanting to remove them, you can make use of "trim" http://php.net/manual/en/function.trim.php

it'd be as simple as:
$string = trim($string, "_");
For future references.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Total brainfart, please help, don't allow spaces.

Post by Temor »

Dylan wrote:I realize you have stated your problem being solved or whatever, however I still feel it is worth mentioning.

You said you wanted to disallow the characters (_) at the beginning and end of the word;
assuming you are just wanting to remove them, you can make use of "trim" http://php.net/manual/en/function.trim.php

it'd be as simple as:
$string = trim($string, "_");
For future references.
Alright. It seems to be doing the trick. Thank you :)
User avatar
Dylan
Posts: 150
Joined: Fri May 06, 2011 7:14 pm

Re: Total brainfart, please help, don't allow spaces.

Post by Dylan »

No problem; the only thing is that removes them.

You would not want to just do that if you'd prefer to notify your users instead of just changing things.
Post Reply