Edit report at http://bugs.php.net/bug.php?id=26244&edit=1

 ID:                 26244
 Comment by:         lukas_panek at yahoo dot com
 Reported by:        willn at umich dot edu
 Summary:            regular expression order matters when it shouldn't
 Status:             Bogus
 Type:               Bug
 Package:            Regexps related
 Operating System:   Solaris 5.8
 PHP Version:        4.3.2
 Block user comment: N
 Private report:     N

 New Comment:

Thank you.

Escaping the "-" character with a bakslash works also.

e.g.: preg_match('/^<!--([a-zA-Z_\-\.]*)-->/',$radka,$match)

Wouldnt have found out without this thread

L


Previous Comments:
------------------------------------------------------------------------
[2003-11-13 15:05:13] j...@php.net

The '-' character means range when it's between the 

[ and ]. You need to put it as the first character or the 

second character between the [^ and ], which is why $b, 

$c, $d and $e work fine. 

 

J 

------------------------------------------------------------------------
[2003-11-13 14:36:47] willn at umich dot edu

Description:
------------
Within the "anti-match" square brackets, order shouldn't make a
difference, but it does. I'm writing a simple anti-injection attack
filter to pull out all the illegal characters, and leave the ones that I
want. Unfortunately, I found that with this particular combination, I've
been getting this error message:



[Thu Nov 13 14:25:41 2003] [error] PHP Warning:  Compilation failed:
range out of order in character class at offset 4 in
/usr/local/projects/vote-dev/public/test.php on line 5



The code below demonstrates the simplest case I could find that would
replicate the problem. Swapping the order a little bit makes it work a
little better - as you can see that the $a and $f variables have some
issues.



Reproduce code:
---------------
<pre><?php

    $text = 'This, is my - "favorite".';

    $a = preg_replace( '/[^,-"]/', '', $text );

    $b = preg_replace( '/[^,"-]/', '', $text );

    $c = preg_replace( '/[^-,"]/', '', $text );

    $d = preg_replace( '/[^-",]/', '', $text );

    $e = preg_replace( '/[^",-]/', '', $text );

    $f = preg_replace( '/[^"-,]/', '', $text );

    echo "$text\na:$a\nb:$b\nc:$c\nd:$d\ne:$e\nf:$f";

?></pre>

Expected result:
----------------
This, is my - "favorite".

a:,-""

b:,-""

c:,-""

d:,-""

e:,-""

f:,-""

Actual result:
--------------
This, is my - "favorite".

a:

b:,-""

c:,-""

d:,-""

e:,-""

f:,""


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=26244&edit=1

Reply via email to