Jump to content

[SOLVED] input only allow certain format


EchoFool

Recommended Posts

I need some help i need to find a way in php to check if a string contains only numbers or letters or the symbol _

 

I know how to check if the string is_numeric but don't how to make it check for symbols other than the _ symbol...

 

What function does this, hope you can help.

Link to comment
https://forums.phpfreaks.com/topic/88252-solved-input-only-allow-certain-format/
Share on other sites

So how come this line:

if(preg_match("/^\w$/",$text)){

 

has like a random w in it etc could explain what the symbols all do.. like is it check that the symbols are in the string .. if so do i have to add more symbols to it i want less symbols to be "allowed"?

First note, I forgot a +...so it should be

preg_match("/^\w+$/",$text)

 

the function is pretty basic, you give it a pattern and a string to match against. Read more here

 

The pattern is a regular expression pattern. Here is a reference sheet on all the symbols: http://www.regular-expressions.info/reference.html

 

Breakdown of the one I provided:

/ => Starts the pattern

^ => Forces the pattern to match from the beginning of the string

\w => Special character, or shorthand, for matching a character, number, or underscore

+ => Means there must be one or more of the previous character (\w)

$ => Forces the pattern to match until the end of the string

/ => Ends the pattern

 

For more Regular Expression help, check out http://www.phpfreaks.com/forums/index.php/board,43.0.html

so if i wanted it to check for only numbers and letters... it would be:

 

preg_match("/^$/",$text)

 

 

Also

with regards to this:

+ => Means there must be one or more of the previous character (\w)

 

Does that mean user names "MUST" have an underscore symbol? or have I mis-understood?

preg_match("/^$/",$text)

That would only match an empty string, because you haven't really specified anything to match. The \w stands for any character OR any number OR an underscore. The + means match one or more of any of those three types. The symbols for Regular Expressions can be difficult to understand at first. The http://www.regular-expressions.info/ site has good tutorials, examples, and reference sheets.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.