ereg_replace question

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


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-18-2006
fakefaker123@hotmail.com
 
Posts: n/a
Default 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

Reply With Quote
  #2 (permalink)  
Old 05-18-2006
Andy Jeffries
 
Posts: n/a
Default Re: ereg_replace question

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

Reply With Quote
  #3 (permalink)  
Old 05-19-2006
Alan Little
 
Posts: n/a
Default 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:

> 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/
Reply With Quote
  #4 (permalink)  
Old 05-22-2006
Andy Jeffries
 
Posts: n/a
Default Re: ereg_replace question

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

Reply With Quote
  #5 (permalink)  
Old 05-22-2006
Alan Little
 
Posts: n/a
Default 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:

> 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/
Reply With Quote
  #6 (permalink)  
Old 05-22-2006
Andy Jeffries
 
Posts: n/a
Default Re: ereg_replace question

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

Reply With Quote
  #7 (permalink)  
Old 05-22-2006
Andy Jeffries
 
Posts: n/a
Default Re: ereg_replace question

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

Reply With Quote
  #8 (permalink)  
Old 05-22-2006
John Dunlop
 
Posts: n/a
Default two kinds of regular expression

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

Reply With Quote
  #9 (permalink)  
Old 05-23-2006
Alan Little
 
Posts: n/a
Default 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:

> 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/
Reply With Quote
  #10 (permalink)  
Old 05-23-2006
Andy Jeffries
 
Posts: n/a
Default Re: ereg_replace question

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

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:34 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0