Re: Seemingly weird regex problem

From: Date: Thu, 20 Jan 2005 20:06:07 +0000
Subject: Re: Seemingly weird regex problem
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-206885@lists.php.net to get a copy of this message
On Thu, 2005-01-20 at 13:21, Tim Boring wrote: > On Thu, 2005-01-20 at 13:41, Jason Wong wrote: > > On Friday 21 January 2005 01:52, Tim Boring wrote: > > > > Well the biggest problem in your code right now is your incomprehensible (to > > me anyway) use of the switch construct. For a start I've no idea why you're > > using ... > > > > > switch ($line) > > > > ... when your tests do not involve $line > > > > > case ($total_counter <= 5): > > I see your point, but the other tests do involve $line. This one that > you reference I'm using for a "special need"--basically so I leave in > the initial column headings from the report, because they would match > several of the tests that would discard those lines. > > > > > 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. > > I say this because I have since changed the first word in each line from > something like "AKRN" to a numeric value, and everything works just as I > would expect it to. So it seems as if the "^" might be negating the \W+ > part of the regex. Although that shouldn't be happening because "^" > only acts as a negation when it's used inside brackets, at least > according the documentation. > > Again, thanks for the suggestion! > I think I see a possible explanation for the behavior. preg_replace does not return a true or false value it returns the string passed as the subject with any matched replacements done. Hmm the manual says it better: If matches are found, the new subject will be returned, otherwise subject will be returned unchanged. What I think is happening is that the first match is not finding anything to replace and returning $line unchanged. the comparison is true since you are comparing to $line in the switch statement. I will p[lay with it a bit since it pisses me off when I can't figure out what is happening. Bret

« previous php.general (#206885) next »