Jump to content

Filter string


birbal

Recommended Posts

i want to customize that in my register page..only a-z,1-0 can be stored...how to filter the input from user. if user input other than a-z or 1-0 ..it will return a error..will the filter regexp work on it..give some idea on it.

Link to comment
Share on other sites

You can use preg_match to find characters that do (or don't) match your pattern.
ok can you please give some code and ilaborate it or give me any link for preg_match tutoriali find in php.net syt bt..it i am not getting all the things like pattern,delimeter and use of it..
Link to comment
Share on other sites

There's a fair amount to know about regular expressions, but this describes the pattern syntax that PHP uses:http://www.php.net/manual/en/reference.pcr...tern.syntax.phpThis site also has a lot of information:http://www.regular-expressions.info/It sounds like you want to use character classes, so something like this would match any lowercase letter or digit:[a-z0-9]

Link to comment
Share on other sites

that link was good but i felt this is little wired thing. it is more complex than i thought..:)i figured out a code for me. please correct me. i am not sure it will work or notobjective:1) all will be lowercase(a-z) or numaric(0-9) and underscore,dash,fullstop. other will will not be accepted.2) minimum 3 letters maximum 10 letters.3) input will be case sensetive.4) it will seacrh entire string and ensure there is not any other letter than i described in 1code:

if(preg_match("/[^a-z0-9_.-]{3,10}/",$inputstring)){//wrong word}else{//right word}

1)what is diffrenece beetwin preg_match() and preg_match_all()?

Link to comment
Share on other sites

I see I have confused you. This code will tell you if any character in the string is not a valid character. The match process stops as soon as one bad character is found.

if(preg_match("/[^a-z0-9_.-]/",$inputstring)){//wrong word}else{//right word}

Adding the quantifier {3,10} to this kind of match will not work, because the test looks for characters that do not match, not strings that do match. I think you did not mention the length requirements before. With a few changes, you can have that too. Try this regex instead:/^[a-z0-9_.-]{3,10}$/This expression wants to match a string between 3-10 characters long, containing only [a-z0-9_.-], and the beginning of the match must be the beginning of the string, and the end of the match must be the end of the string.The ^ and $ do that last part, which is important. Without it, the following would be a match:!@#(HELLO.1234****Because HELLO.1234 fits the pattern. When we add the ^ and $ controls, the extra characters cause the string to fail. But this by itself will still match:HELLO.1234Be careful about the placement of the ^ character. It means different things when it is inside [brackets] and when it is outside them. For this new regex, it must come outside the brackets.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...