ereg_replace question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fakefaker123@hotmail.com

    #1

    ereg_replace question

    Hi,
    Does anyone know what exactly this does: ereg_replace("[^0-9.]", "",
    $value)
    would this be responsible for a negative number losing it's minus sign?
    e.g, if @value is minus 2300.00 (-2300.00), then after the above
    function, is it still = -2300.00?
    thanks

  • Andy Jeffries

    #2
    Re: ereg_replace question

    On Thu, 18 May 2006 10:12:53 -0700, fakefaker123 wrote:[color=blue]
    > Hi,
    > Does anyone know what exactly this does:[/color]

    Broken down:

    ereg_replace("
    Replace using simple regular expressions

    [
    A character in the following range

    ^
    Anything but...

    0-9
    The digits 0-9

    ..
    A full stop or decimal point

    ]
    End of the range

    ", "",
    With an empty string (i.e. delete)

    $value)
    In $value

    So it removes any character from $value that isn't 0-9 or .
    [color=blue]
    > would this be responsible for a negative number losing it's minus sign?[/color]

    Yes. The - sign isn't listed in the range (i.e. it would be [^0-9.\-]) so
    it will remove it from the output
    [color=blue]
    > e.g, if @value is minus 2300.00 (-2300.00), then after the above
    > function, is it still = -2300.00?[/color]

    Nope, it'll be 2300.00

    Cheers,


    Andy

    --
    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
    http://www.gphpedit.org | PHP editor for Gnome 2
    http://www.andyjeffries.co.uk | Personal site and photos

    Comment

    • Alan Little

      #3
      Re: ereg_replace question

      Carved in mystic runes upon the very living rock, the last words of Andy
      Jeffries of comp.lang.php make plain:
      [color=blue]
      > Yes. The - sign isn't listed in the range (i.e. it would be
      > [^0-9.\-]) so it will remove it from the output[/color]

      In brackets, you only escape the [] characters themselves. Your expression
      will allow the \ character in the string.

      --
      Alan Little
      Phorm PHP Form Processor

      Comment

      • Andy Jeffries

        #4
        Re: ereg_replace question

        On Thu, 18 May 2006 20:56:59 -0500, Alan Little wrote:[color=blue]
        > Carved in mystic runes upon the very living rock, the last words of Andy
        > Jeffries of comp.lang.php make plain:
        >[color=green]
        >> Yes. The - sign isn't listed in the range (i.e. it would be [^0-9.\-])
        >> so it will remove it from the output[/color]
        >
        > In brackets, you only escape the [] characters themselves. Your expression
        > will allow the \ character in the string.[/color]

        I appreciate your enthusiasm but did you actually test this before trying
        to correct me ;-)

        $ cat test.php
        <?php print preg_match("/[\-]/", '\\')."\n"; ?>
        $ php test.php
        0

        According to your login, the \ in the string should have matched against
        either "\ or -", but unsurprisingly it didn't.

        However, you do make the good point that you don't *need* to escape the -,
        I just do it for readability so there's no mistaking that I meant a
        literal dash and not part of a range that I forgot to complete.

        Cheers,


        Andy

        --
        Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
        http://www.gphpedit.org | PHP editor for Gnome 2
        http://www.andyjeffries.co.uk | Personal site and photos

        Comment

        • Alan Little

          #5
          Re: ereg_replace question

          Carved in mystic runes upon the very living rock, the last words of Andy
          Jeffries of comp.lang.php make plain:
          [color=blue]
          > On Thu, 18 May 2006 20:56:59 -0500, Alan Little wrote:[color=green]
          >> Carved in mystic runes upon the very living rock, the last words of
          >> Andy Jeffries of comp.lang.php make plain:
          >>[color=darkred]
          >>> Yes. The - sign isn't listed in the range (i.e. it would be
          >>> [^0-9.\-]) so it will remove it from the output[/color]
          >>
          >> In brackets, you only escape the [] characters themselves. Your
          >> expression will allow the \ character in the string.[/color]
          >
          > I appreciate your enthusiasm but did you actually test this before
          > trying to correct me ;-)[/color]

          Yes, I did. However, I checked with ereg(), which does allow the \
          character, given the expression you posted earlier.
          [color=blue]
          > However, you do make the good point that you don't *need* to escape
          > the -, I just do it for readability so there's no mistaking that I
          > meant a literal dash and not part of a range that I forgot to
          > complete.[/color]

          OK. I don't believe it's standard, however.

          --
          Alan Little
          Phorm PHP Form Processor

          Comment

          • Andy Jeffries

            #6
            Re: ereg_replace question

            On Mon, 22 May 2006 13:47:39 -0500, Alan Little wrote:[color=blue][color=green][color=darkred]
            >>> In brackets, you only escape the [] characters themselves. Your
            >>> expression will allow the \ character in the string.[/color]
            >>
            >> I appreciate your enthusiasm but did you actually test this before
            >> trying to correct me ;-)[/color]
            >
            > Yes, I did. However, I checked with ereg(), which does allow the \
            > character, given the expression you posted earlier.[/color]

            Ah, ereg isn't standard, it's PHP dodgy partial regular expression engine.
            [color=blue][color=green]
            >> However, you do make the good point that you don't *need* to escape the
            >> -, I just do it for readability so there's no mistaking that I meant a
            >> literal dash and not part of a range that I forgot to complete.[/color]
            >
            > OK. I don't believe it's standard, however.[/color]

            AFAIK Perl is the standard for regular expression implementation and:

            $ cat test.pl
            #!/usr/bin/perl
            $string = "\\";
            if ($string =~ /[\-]/) {
            print "matches\n" ;
            }
            else {
            print "doesn't match\n";
            }
            $ ./test.pl
            doesn't match

            Cheers,


            Andy


            --
            Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
            http://www.gphpedit.org | PHP editor for Gnome 2
            http://www.andyjeffries.co.uk | Personal site and photos

            Comment

            • Andy Jeffries

              #7
              Re: ereg_replace question

              On Mon, 22 May 2006 13:47:39 -0500, Alan Little wrote:[color=blue][color=green]
              >> However, you do make the good point that you don't *need* to escape the
              >> -, I just do it for readability so there's no mistaking that I meant a
              >> literal dash and not part of a range that I forgot to complete.[/color]
              >
              > OK. I don't believe it's standard, however.[/color]

              On p79 of Mastering Regular Expressions by Jeffrey E F Friedl (ISBN
              1-56592-257-3) it says:

              "In limited-metacharacter-class implementations , other metacharacter
              (including in most tools, even backslashes) are not recognized. So, for
              example, you can't use \- or \] to insert a hyphen or a closing bracket in
              to the class." This precedes a list of characters that are available in
              these limited implementations which are specifically: a leading caret, the
              closing bracket and a dash as a range operator.

              I'm sure that book details the "standard" for regular expressions in most
              people's eyes and that book (as quoted above) uses \- as the syntax to
              insert a literal hyphen with a metacharacter class ([...]).

              So it would seem that while [^0-9-] works in PHP/Perl, it's actually not
              standard and I am correct to use [^0-9\-] in order to ensure maximum
              compatibility with future version which may implement the standard more
              strictly.

              Cheers,



              Andy

              --
              Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
              http://www.gphpedit.org | PHP editor for Gnome 2
              http://www.andyjeffries.co.uk | Personal site and photos

              Comment

              • John Dunlop

                #8
                two kinds of regular expression

                Andy Jeffries:
                [color=blue]
                > So it would seem that while [^0-9-] works in PHP/Perl, it's actually not
                > standard and I am correct to use [^0-9\-] in order to ensure maximum
                > compatibility with future version which may implement the standard more
                > strictly.[/color]

                I'd not say you're correct, and I'd shy away from speaking about
                *the* "standard", whatever you mean by that. Where there's two kinds
                of regular expression, claiming that one is standard implies the other
                is not, forcing upon it gratuitous negative connotations. If you do
                feel the urge to think in terms of standard/non-standard, don't think
                of there being one standard and one non-standard but rather of there
                being two standards.

                You pays your money and you takes your choice.

                --
                Jock

                Comment

                • Alan Little

                  #9
                  Re: ereg_replace question

                  Carved in mystic runes upon the very living rock, the last words of Andy
                  Jeffries of comp.lang.php make plain:
                  [color=blue]
                  > On Mon, 22 May 2006 13:47:39 -0500, Alan Little wrote:[color=green][color=darkred]
                  >>> However, you do make the good point that you don't *need* to escape
                  >>> the -, I just do it for readability so there's no mistaking that I
                  >>> meant a literal dash and not part of a range that I forgot to
                  >>> complete.[/color]
                  >>
                  >> OK. I don't believe it's standard, however.[/color]
                  >
                  > On p79 of Mastering Regular Expressions by Jeffrey E F Friedl (ISBN
                  > 1-56592-257-3) it says:
                  >
                  > "In limited-metacharacter-class implementations , other metacharacter
                  > (including in most tools, even backslashes) are not recognized. So,
                  > for example, you can't use \- or \] to insert a hyphen or a closing
                  > bracket in to the class." This precedes a list of characters that are
                  > available in these limited implementations which are specifically: a
                  > leading caret, the closing bracket and a dash as a range operator.
                  >
                  > I'm sure that book details the "standard" for regular expressions in
                  > most people's eyes and that book (as quoted above) uses \- as the
                  > syntax to insert a literal hyphen with a metacharacter class ([...]).
                  >
                  > So it would seem that while [^0-9-] works in PHP/Perl, it's actually
                  > not standard and I am correct to use [^0-9\-] in order to ensure
                  > maximum compatibility with future version which may implement the
                  > standard more strictly.[/color]

                  That's a good reference, but I don't follow you. The part you quoted from
                  the book says you *can't* use \- to insert a hyphen in the class.

                  --
                  Alan Little
                  Phorm PHP Form Processor

                  Comment

                  • Andy Jeffries

                    #10
                    Re: ereg_replace question

                    On Mon, 22 May 2006 19:52:24 -0500, Alan Little wrote:[color=blue][color=green]
                    >> On p79 of Mastering Regular Expressions by Jeffrey E F Friedl (ISBN
                    >> 1-56592-257-3) it says:
                    >>
                    >> "In limited-metacharacter-class implementations , other metacharacter
                    >> (including in most tools, even backslashes) are not recognized. So, for
                    >> example, you can't use \- or \] to insert a hyphen or a closing bracket
                    >> in to the class." This precedes a list of characters that are available
                    >> in these limited implementations which are specifically: a leading
                    >> caret, the closing bracket and a dash as a range operator.
                    >>
                    >> I'm sure that book details the "standard" for regular expressions in
                    >> most people's eyes and that book (as quoted above) uses \- as the syntax
                    >> to insert a literal hyphen with a metacharacter class ([...]).
                    >>
                    >> So it would seem that while [^0-9-] works in PHP/Perl, it's actually not
                    >> standard and I am correct to use [^0-9\-] in order to ensure maximum
                    >> compatibility with future version which may implement the standard more
                    >> strictly.[/color]
                    >
                    > That's a good reference, but I don't follow you. The part you quoted from
                    > the book says you *can't* use \- to insert a hyphen in the class.[/color]

                    In case it's not clear, that's a book on Regular Expressions and not
                    specifically about PHP regexes.

                    In a *limited-metacharacter-class implementation* . Those implementations
                    can only accept leading caret, closing bracket and a hyphen as a range
                    character (i.e. there's no way to find a hyphen, a slash or any other
                    non-alphanumeric character). PHP is not a limited-metacharacter-class
                    implementation.

                    Cheers,


                    Andy


                    --
                    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                    http://www.gphpedit.org | PHP editor for Gnome 2
                    http://www.andyjeffries.co.uk | Personal site and photos

                    Comment

                    • Andy Jeffries

                      #11
                      Re: two kinds of regular expression

                      On Mon, 22 May 2006 14:38:52 -0700, John Dunlop wrote:[color=blue][color=green]
                      >> So it would seem that while [^0-9-] works in PHP/Perl, it's actually not
                      >> standard and I am correct to use [^0-9\-] in order to ensure maximum
                      >> compatibility with future version which may implement the standard more
                      >> strictly.[/color]
                      >
                      > I'd not say you're correct[/color]

                      OK, so ignoring the latter part about referring to standards, why am I not
                      correct?
                      [color=blue]
                      > and I'd shy away from speaking about
                      > *the* "standard", whatever you mean by that. Where there's two kinds of
                      > regular expression, claiming that one is standard implies the other is
                      > not, forcing upon it gratuitous negative connotations. If you do feel the
                      > urge to think in terms of standard/non-standard, don't think of there
                      > being one standard and one non-standard but rather of there being two
                      > standards.[/color]

                      OK, considering the two main standards out there (PCRE and POSIX), both of
                      them suggest literal hyphens should be quoted within metcharacter classes.
                      The main book most people use to refer to Regular Expressions suggests the
                      same thing.

                      While I understand there's no one true standard for regexes, the nearest
                      things we have say it should be done one way, therefore although method B
                      also works, if it's not in any references it may be just an oversight that
                      will be removed in a latter revision of the code.

                      Cheers,


                      Andy


                      --
                      Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                      http://www.gphpedit.org | PHP editor for Gnome 2
                      http://www.andyjeffries.co.uk | Personal site and photos

                      Comment

                      • Alan Little

                        #12
                        Re: ereg_replace question

                        Carved in mystic runes upon the very living rock, the last words of Andy
                        Jeffries of comp.lang.php make plain:
                        [color=blue]
                        > On Mon, 22 May 2006 19:52:24 -0500, Alan Little wrote:[color=green][color=darkred]
                        >>> On p79 of Mastering Regular Expressions by Jeffrey E F Friedl (ISBN
                        >>> 1-56592-257-3) it says:
                        >>>
                        >>> "In limited-metacharacter-class implementations , other metacharacter
                        >>> (including in most tools, even backslashes) are not recognized. So,
                        >>> for example, you can't use \- or \] to insert a hyphen or a closing
                        >>> bracket in to the class." This precedes a list of characters that
                        >>> are available in these limited implementations which are
                        >>> specifically: a leading caret, the closing bracket and a dash as a
                        >>> range operator.
                        >>>
                        >>> I'm sure that book details the "standard" for regular expressions in
                        >>> most people's eyes and that book (as quoted above) uses \- as the
                        >>> syntax to insert a literal hyphen with a metacharacter class
                        >>> ([...]).
                        >>>
                        >>> So it would seem that while [^0-9-] works in PHP/Perl, it's actually
                        >>> not standard and I am correct to use [^0-9\-] in order to ensure
                        >>> maximum compatibility with future version which may implement the
                        >>> standard more strictly.[/color]
                        >>
                        >> That's a good reference, but I don't follow you. The part you quoted
                        >> from the book says you *can't* use \- to insert a hyphen in the
                        >> class.[/color]
                        >
                        > In case it's not clear, that's a book on Regular Expressions and not
                        > specifically about PHP regexes.[/color]

                        I understand.
                        [color=blue]
                        > In a *limited-metacharacter-class implementation* . Those
                        > implementations can only accept leading caret, closing bracket and a
                        > hyphen as a range character (i.e. there's no way to find a hyphen, a
                        > slash or any other non-alphanumeric character). PHP is not a
                        > limited-metacharacter-class implementation.[/color]

                        Pardon my density, but I still don't follow you. The book says:
                        [color=blue][color=green][color=darkred]
                        >>> "So, for example, you can't use \- or \] to insert a hyphen or a
                        >>> closing bracket in to the class."[/color][/color][/color]

                        You say:
                        [color=blue][color=green][color=darkred]
                        >>> I am correct to use [^0-9\-] in order to ensure[/color][/color][/color]

                        The book says it's incorrect, but you're saying it's correct? Am I
                        missing something?

                        --
                        Alan Little
                        Phorm PHP Form Processor

                        Comment

                        • Andy Jeffries

                          #13
                          Re: ereg_replace question

                          On Tue, 23 May 2006 06:11:02 -0500, Alan Little wrote:[color=blue][color=green][color=darkred]
                          >>>> "In limited-metacharacter-class implementations , other metacharacter
                          >>>> (including in most tools, even backslashes) are not recognized. So,
                          >>>> for example, you can't use \- or \] to insert a hyphen or a closing
                          >>>> bracket in to the class." This precedes a list of characters that are
                          >>>> available in these limited implementations which are specifically: a
                          >>>> leading caret, the closing bracket and a dash as a range operator.[/color][/color]
                          >
                          > Pardon my density, but I still don't follow you. The book says:
                          >[color=green][color=darkred]
                          >>>> "So, for example, you can't use \- or \] to insert a hyphen or a
                          >>>> closing bracket in to the class."[/color][/color]
                          >
                          > You say:
                          >[color=green][color=darkred]
                          >>>> I am correct to use [^0-9\-] in order to ensure[/color][/color]
                          >
                          > The book says it's incorrect, but you're saying it's correct? Am I missing
                          > something?[/color]

                          The book is saying in (limited, non-full, implementations ) you cannot use
                          "\-" to insert a hyphen as you cannot search for a hyphen as one of the
                          characters in a metaclass.

                          It gives an example (which I paraphrased) of the only acceptable
                          characters in a limited implementation and basically you can't include (in
                          any shape or form) hyphens or square brackets in the class.

                          So the book said "in these limited forms you can't use \- to insert a
                          hyphen", which by the phrasing indicates that's the normal way of doing it
                          in a full implementation.

                          PHP is a full PCRE implementation with all bells and whistles (including
                          backreferences) .

                          Does that make more sense?

                          Cheers,


                          Andy


                          --
                          Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                          http://www.gphpedit.org | PHP editor for Gnome 2
                          http://www.andyjeffries.co.uk | Personal site and photos

                          Comment

                          • Alan Little

                            #14
                            Re: ereg_replace question

                            Carved in mystic runes upon the very living rock, the last words of Andy
                            Jeffries of comp.lang.php make plain:
                            [color=blue]
                            > So the book said "in these limited forms you can't use \- to insert a
                            > hyphen", which by the phrasing indicates that's the normal way of
                            > doing it in a full implementation.
                            >
                            > PHP is a full PCRE implementation with all bells and whistles
                            > (including backreferences) .[/color]

                            OK, I see what you're saying.

                            --
                            Alan Little
                            Phorm PHP Form Processor

                            Comment

                            • John Dunlop

                              #15
                              Re: two kinds of regular expression

                              Andy Jeffries:
                              [color=blue]
                              > OK, so ignoring the latter part about referring to standards, why am I not
                              > correct?[/color]

                              I didn't say you weren't correct. I just don't see why saying that
                              you are correct helps.
                              [color=blue]
                              > OK, considering the two main standards out there (PCRE and POSIX), both of
                              > them suggest literal hyphens should be quoted within metcharacter classes.[/color]

                              I don't know what you mean. The notation of POSIX regular
                              expressions does not suggest anything of the sort but actually *rules*
                              *out* backslashes as escape characters in character classes. The man
                              pages are quite explicit: backslashes lose their metacharacter
                              function there. The notation of PCREs does allow backslashes as escape
                              characters in character classes but also allows literal hyphens to
                              occur in certain positions unescaped. I don't see how it follows from
                              that that the notation used by either kind of regular expression, let
                              alone both, suggests that literal hyphens *should* be escaped.

                              --
                              Jock

                              Comment

                              Working...