Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_match

Status
Not open for further replies.

edpatterson

IS-IT--Management
Feb 24, 2005
186
I am rather confused about just how the preg_match functions.
<?
$string1 = '#123&abc';
$string2 = '123abc';
$string3 = '123 456';

preg_match('/[[:alnum:]]/',$string1)
returns 1
preg_match('/[[:alnum:]]/',$string2)
returns 1
preg_match('/[[:alnum:]]/',$string3)
returns 1

I had expected only $string2 to be true.

What am I missing? Just trying to do some basic error checking.

Ed
 
Your pattern matches any single alphanumeric character. preg_match() is indicating, correctly, that each of the three strings has at least one alphanumeric character in it.

I don't understand why you are expecting only $string2 to match. What are you trying to find in the strings?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
:)

I expected a match if the string contained _only_ alphanumeric characters. That is why I thought only 2 would be true.

I read the online manual and that is what lead me to beleive it would work. I also looked it in my my O'Reilly and APress books. I guess I will run to Borders and get a couple more.

Ed
 
The pattern you will want is one that, in English, would read, "A string that is nothing but alphanumeric characters from front to back".

For that, you might try:

preg_match('/^[[:alnum:]]+$/',$string1)

Which would require that the string consist of at least one alphanumeric character. The statement:

preg_match('/^[[:alnum:]]*$/',$string1)

would allow for zero-length strings.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks!
I had tried the + but achived the exact same results, everything was true. Now I am scratching my head as to why the begin of string and end of string markers make a difference.

I'll read and experiment some more to see if I can figure it out.

Ed
 
because the other strings (1 and 3) have non alphanumeric characters in.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
The beginning-of-string and end-of-string characters have to be there. They force preg_match to match the entire string, not just look for a single character withing the string.

Again, your pattern, in English, says, "match a single alphanumeric character". Mine, in English, says, "Start at the beginning of the string. Match alphanumeric characters until you get to the end of the string". If any non-alphanumeric characters appear in the string, preg_match() can't match what I've told it to.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
KarveR, you missed the point of my quandry. I realize the other strings have non alphanumeric characters in them, I typed them. I don't understand why the '/[[:alnum]]+/' did not work but the '/^[[:alnum:]]+$/' did.

I think more reading is in order.

Ed
 
edpatterson:

All the pattern '/[[:alnum:]]+/' means is "regardless of the position in the string, match one or more alphanumeric characters in a row". Any non-zero-length string that contains a single alphanumeric character anywhere within it will match the pattern.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
DING! the light bulb goes on!

Thanks sleipnir214 for the excellent explaination.

Ed
 
Sorry Ed, I didn't miss it, I just couldn't think of the best way to explain to you why it wasn't working, when I looked at it I knew why ... Sleipnir strikes again :)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
It just occurred to me that there may be a way of detecting the strings that requires a simpler pattern.

"A string comprised entirely of alphanumeric characters" is the same as "as string that doesn't contain any non-alphanumeric chacters"

This that in mind, perhaps using a pattern of:

/[^[:alnum:]]/

And then checking that the pattern was NOT matched.....

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top