Total brainfart, please help, don't allow spaces.
Total brainfart, please help, don't allow spaces.
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.
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.
$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.
Well, that's a start at least.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
Now I got the name of the function. Php.net here I come
Thank you.
Re: Total brainfart, please help, don't allow spaces.
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.
I tried preg_match. But reading php.net didn't help me understand it.jacek wrote:preg_replace will replace any thing it matches, preg_match will tell you if a match happened.
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.
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 ?
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.
I can't seem to figure out what I'm supposed to put in it, and in what order.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 ?
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.
if your looking for a space character. i think you need to use \s
Re: Total brainfart, please help, don't allow spaces.
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.
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 spacesif(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.
That did it.jacek wrote:Try
[syntax]/\s+/[/syntax]
as the pattern.
Jacek, you're the best! Thank you
Re: Total brainfart, please help, don't allow spaces.
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:
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.
Alright. It seems to be doing the trick. Thank youDylan 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.
Re: Total brainfart, please help, don't allow spaces.
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.
You would not want to just do that if you'd prefer to notify your users instead of just changing things.