473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

May 18 '06 #1
14 1900
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

May 18 '06 #2
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/
May 19 '06 #3
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

May 22 '06 #4
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/
May 22 '06 #5
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

May 22 '06 #6
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

May 22 '06 #7
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

May 22 '06 #8
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/
May 23 '06 #9
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

May 23 '06 #10
On Mon, 22 May 2006 14:38:52 -0700, John Dunlop wrote:
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


OK, so ignoring the latter part about referring to standards, why am I not
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.


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

May 23 '06 #11
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 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.


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


Pardon my density, but I still don't follow you. The book says:
"So, for example, you can't use \- or \] to insert a hyphen or a
closing bracket in to the class."
You say:
I am correct to use [^0-9\-] in order to ensure


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

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 23 '06 #12
On Tue, 23 May 2006 06:11:02 -0500, Alan Little wrote:
"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.
Pardon my density, but I still don't follow you. The book says:
"So, for example, you can't use \- or \] to insert a hyphen or a
closing bracket in to the class."
You say:
I am correct to use [^0-9\-] in order to ensure


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


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

May 23 '06 #13
Carved in mystic runes upon the very living rock, the last words of Andy
Jeffries of comp.lang.php make plain:
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).


OK, I see what you're saying.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 23 '06 #14
Andy Jeffries:
OK, so ignoring the latter part about referring to standards, why am I not
correct?
I didn't say you weren't correct. I just don't see why saying that
you are correct helps.
OK, considering the two main standards out there (PCRE and POSIX), both of
them suggest literal hyphens should be quoted within metcharacter classes.


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

May 23 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roland Sippel | last post by:
Is there anything change in "ereg_replace()" PHP > 4.3.2. Scripts works in PHP 4.3.1 but NOT in PHP 4.3.2: $text = "<!-- test -->"; $text = ereg_replace("]+"," ",strip_tags($text)); echo $text...
2
by: Chris | last post by:
Hello, I use a javascript ticker in my header which contains the news in a array like that tickercontents='Message1' tickercontents='Message2' tickercontents='Message3' now i want to write an...
4
by: knocte | last post by:
Hello. I am testing this curious code and I don't understand yet why it doesn't work with the 2nd case: Case 1: $number = "2,4"; $number = ereg_replace(",",".",$number); echo $number; //...
0
by: Sean Pinto | last post by:
i am designing a search engine for our website. i want to allow for searchablity based on partial string match as well as quoted exact matches. for example the search search text many would...
3
by: Xaradas | last post by:
Someone could tell me why preg_replace eat a slash when it do replacement? <? $stringToReplace = "blah blah {nome} blah blah"; $replacement = "Two slash: \\\\"; $stringToReplace =...
0
by: pieterprovoost | last post by:
Hi, I would like to use the ereg_replace() function to replace wiki-like syntax, for example: ] becomes <a href="index.php?page=somepage">somepage</a> **hello** becomes <i>hello</i>
1
by: tmcp | last post by:
Hello I'm a bit new to regular expressions and I'm having a problem I'm trying to strip any code which is inside a <p> tag for instance from <p style="margin: 0in 0in 0pt;" class="MsoNormal"> to...
2
by: encepif | last post by:
I think this is the right command., could someone please show me how to replace instances of a quote like this " with its html version - &quot;. I am mixed up with the escaping, etc. Thank you :-) ...
5
by: Hugh Oxford | last post by:
I have a string that has been saved in a database from a textarea form field. e.g. $text = "Dear %name The date is %date yours,
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.