Checking string format with regex in PHP

varkey

New Member
I have been trying to use preg_match with \[code\]"/^[a-zA-Z0-9_-]+$/"\[/code\] trying to make sure that the string given only contained the characters there and nothing else, but it seems to not be counting the $ properly so that it matches all the way to the end of the line, but it does require the match at the start of the string using ^.If I use \[code\]{3,}\[/code\] instead of \[code\]+\[/code\]: \[code\]"/^[a-zA-Z0-9_-]{3,}$/"\[/code\] which is how I had it at first, I could write 3 letters/numbers, - or _ and then any character other than those and it would count it as a match.code:\[code\] if(preg_match("/^[a-zA-Z0-9_-]+$/", $value, $arr) && strlen($value) > 3) { echo $good .' Ok'; } else { echo $bad .' Invalid username. (letters & numbers only)'; }\[/code\]using things like the following as $value, it tells me it is ok when it should be coming up as invalid username because of the space or & characters.\[quote\] word word word&word\[/quote\]And it turns out it was because the values were being sent to the page through $_GET and $value of something such as "word&word" came up as "word" instead... is there a way to fix that?
 
Back
Top