Page 1 of 1
Total brainfart, please help, don't allow spaces.
Posted: Mon Jun 27, 2011 10:29 pm
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
/Edit: Posting my code is useless since its only an input field and an if statement to check if its been set.
Re: Total brainfart, please help, don't allow spaces.
Posted: Mon Jun 27, 2011 10:37 pm
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
Re: Total brainfart, please help, don't allow spaces.
Posted: Mon Jun 27, 2011 10:39 pm
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.
Re: Total brainfart, please help, don't allow spaces.
Posted: Tue Jun 28, 2011 9:53 am
by jacek
preg_replace will replace any thing it matches, preg_match will tell you if a match happened.
Re: Total brainfart, please help, don't allow spaces.
Posted: Tue Jun 28, 2011 2:55 pm
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
Re: Total brainfart, please help, don't allow spaces.
Posted: Tue Jun 28, 2011 3:43 pm
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 ?
Re: Total brainfart, please help, don't allow spaces.
Posted: Tue Jun 28, 2011 9:27 pm
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 ?
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
Re: Total brainfart, please help, don't allow spaces.
Posted: Tue Jun 28, 2011 10:32 pm
by Torniquet
if your looking for a space character. i think you need to use \s
Re: Total brainfart, please help, don't allow spaces.
Posted: Wed Jun 29, 2011 9:38 am
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.
}
Re: Total brainfart, please help, don't allow spaces.
Posted: Wed Jun 29, 2011 3:14 pm
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.
Re: Total brainfart, please help, don't allow spaces.
Posted: Thu Jun 30, 2011 4:42 pm
by jacek
Try
[syntax]/\s+/[/syntax]
as the pattern.
Re: Total brainfart, please help, don't allow spaces.
Posted: Thu Jun 30, 2011 5:40 pm
by Temor
jacek wrote:Try
[syntax]/\s+/[/syntax]
as the pattern.
That did it.
Jacek, you're the best! Thank you
Re: Total brainfart, please help, don't allow spaces.
Posted: Fri Jul 08, 2011 2:41 am
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.
Re: Total brainfart, please help, don't allow spaces.
Posted: Tue Jul 12, 2011 10:47 pm
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
Re: Total brainfart, please help, don't allow spaces.
Posted: Wed Jul 13, 2011 12:59 am
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.