You are here: RE: [PHP] Seemingly weird regex problem « PHP « IT news, forums, messages
RE: [PHP] Seemingly weird regex problem

Posted by Michael Sims on 01/20/05 22:13

Tim Boring wrote:
> On Thu, 2005-01-20 at 13:41, Jason Wong wrote:
>> I suspect what you want to be doing is something like this:
>>
>> switch (TRUE) {
>> case ANY_EXPRESSION_THAT_EVALUATES_TO_TRUE:
>> ...
>> }
>
> Thanks for the suggestion, but I'm not sure that does what I'm looking
> for. I really think the problem is with my regex, not necessarily
> with the way I've constructed my switch statement.

No, Jason is right. Your problem IS in the switch statement. You cannot use it the
way you are trying to and get the results you're expecting. Each case expression is
evaluated with the switch expression just as if you compared them with the "=="
operator. In your case, you're comparing the return value of preg_match() against
the $line string. This isn't what you want.

Here's what happens. Say your $line contains the string "AKRN". Your regex pattern
matches only if the line begins with a non-word character. "A" is definitely a word
character, so the pattern does not match. preg_match returns an integer indicating
the number of matches, which in this case is 0.

To evaluate your case, PHP does the equivalent of:

if ("AKRN..." == 0)

which is actually TRUE. From:

http://www.php.net/manual/en/language.types.string.php#language.types.string.convers
ion

"If the string starts with valid numeric data, this will be the value used.
Otherwise, the value will be 0 (zero)."

Since "AKRN" doesn't begin with valid numeric data, it is converted to 0 for the
purposes of the comparison. Since 0 == 0 the case comparison evaluates to true.

When you switch it to begin with a number, PHP now uses that number. Say you switch
it to "1AKRN". PHP then will compare 1 == 0 which is false.

To accomplish what you want you'll have to change it to:

switch (true) {
case ($total_counter <= 5):
...
case (preg_match(...):
...

etc. as Jason suggested. Why don't you try it and see if it works?

HTH

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация