preg_match/if statements, can't quite grasp it...

jbdesign

New Member
Messages
26
Reaction score
0
Points
0
I'm trying to write an if statement that tests/validates a field that has a two word name, for example New York. I just can't seem to figure out how to do it. I mean was able to write perfectly working if statements to test/validate email addresses, US zip codes, state abbreviations, and so on, I just can't seem to understand getting an if statement that checks for two word names!

Any help would be appreciated.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
When asking about code, post short samples (minimal test cases, actually). Without this, it's hard to help you with mistakes you might be making. Also, describe the overall goal (why are you trying to do whatever it is?) in addition to the specific task. Sometimes there's a better way of achieving the goal, obviating your question.

REs to match a string containing exactly two words:
/^\W+(\w+\W+){2}$/
If you want to restrict strings to word characters and spaces:
/^\s+(\w+\s+){2}$/
If you don't want leading or trailing spaces:
/^\w+\s+\w+$/
 
Last edited:

jbdesign

New Member
Messages
26
Reaction score
0
Points
0
I do apologize for not providing an example or what exactly my desired end result(s) are.

Your re examples were more then helpful and I was able to pinpoint where I was going wrong.

Thank you again fro your help!
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
This post was originally an accidental duplicate. It's now marked for deletion.
 
Last edited:
Top