Re: Seemingly weird regex problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 2005-01-20 at 14:06, Bret Hughes wrote:

> 
> 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.


Well it sure is easier to debug when you are looking at the right
frigging function.  I think the logic error is still similar you are
comparing two values that are not meant to be compared.  

Since if an integer is involved in a comparison, and it is here since
preg_match returns an integer, the string will be converted to an
integer.  IIRC string to integer conversion results in 0 if the string
does not look like number.

Since the match is not found, preg_match returns 0 compared to the
string contained in $line will be converted to 0 ( if it begins with a
letter its integer represetation is 0) the case mathes and the block
executes.

That and the first case is totally misued.  you are trying to use the
switch as a series of ifs and the analogy only holds true if the
comparison is the same.

I would not hesitate to rewrite this and encourage you to do so because
even if you get it to work as intended it is not clear what you are
trying to do.

Bret

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux