This is a discussion on ereg_replace question within the PHP Language forums, part of the PHP Programming Forums category; Hi, Does anyone know what exactly this does: ereg_replace("[^0-9.]", "", $value) would this be responsible ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
On Thu, 18 May 2006 10:12:53 -0700, fakefaker123 wrote:
> Hi, > Does anyone know what exactly this does: 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 . > would this be responsible for a negative number losing it's minus sign? Yes. The - sign isn't listed in the range (i.e. it would be [^0-9.\-]) so it will remove it from the output > e.g, if @value is minus 2300.00 (-2300.00), then after the above > function, is it still = -2300.00? 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 |
|
|||
|
Carved in mystic runes upon the very living rock, the last words of Andy
Jeffries of comp.lang.php make plain: > Yes. The - sign isn't listed in the range (i.e. it would be > [^0-9.\-]) so it will remove it from the output In brackets, you only escape the [] characters themselves. Your expression will allow the \ character in the string. -- Alan Little Phorm PHP Form Processor http://www.phorm.com/ |
|
|||
|
On Thu, 18 May 2006 20:56:59 -0500, Alan Little wrote:
> Carved in mystic runes upon the very living rock, the last words of Andy > Jeffries of comp.lang.php make plain: > >> Yes. The - sign isn't listed in the range (i.e. it would be [^0-9.\-]) >> so it will remove it from the output > > In brackets, you only escape the [] characters themselves. Your expression > will allow the \ character in the string. 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 |
|
|||
|
Carved in mystic runes upon the very living rock, the last words of Andy
Jeffries of comp.lang.php make plain: > On Thu, 18 May 2006 20:56:59 -0500, Alan Little wrote: >> Carved in mystic runes upon the very living rock, the last words of >> Andy Jeffries of comp.lang.php make plain: >> >>> Yes. The - sign isn't listed in the range (i.e. it would be >>> [^0-9.\-]) so it will remove it from the output >> >> In brackets, you only escape the [] characters themselves. Your >> expression will allow the \ character in the string. > > I appreciate your enthusiasm but did you actually test this before > trying to correct me ;-) Yes, I did. However, I checked with ereg(), which does allow the \ character, given the expression you posted earlier. > 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. OK. I don't believe it's standard, however. -- Alan Little Phorm PHP Form Processor http://www.phorm.com/ |
|
|||
|
On Mon, 22 May 2006 13:47:39 -0500, Alan Little wrote:
>>> In brackets, you only escape the [] characters themselves. Your >>> expression will allow the \ character in the string. >> >> I appreciate your enthusiasm but did you actually test this before >> trying to correct me ;-) > > Yes, I did. However, I checked with ereg(), which does allow the \ > character, given the expression you posted earlier. Ah, ereg isn't standard, it's PHP dodgy partial regular expression engine. >> 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. > > OK. I don't believe it's standard, however. 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 |
|
|||
|
On Mon, 22 May 2006 13:47:39 -0500, Alan Little wrote:
>> 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. > > OK. I don't believe it's standard, however. 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 |
|
|||
|
Andy Jeffries:
> 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. 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 |
|
|||
|
Carved in mystic runes upon the very living rock, the last words of Andy
Jeffries of comp.lang.php make plain: > On Mon, 22 May 2006 13:47:39 -0500, Alan Little wrote: >>> 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. >> >> OK. I don't believe it's standard, however. > > 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. 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 http://www.phorm.com/ |
|
|||
|
On Mon, 22 May 2006 19:52:24 -0500, Alan Little wrote:
>> 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. > > 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. 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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|