By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,114 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,114 IT Pros & Developers. It's quick & easy.

ereg_replace(): this drives me nuts

P: n/a
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; // displays "2.4"
Case 2:

$number = "2.4";
$number = ereg_replace(".",",",$number);
echo $number; // displays a strange thing!!!

Anyone know why ereg_replace() is not working? Thanks is advance, and
happy new year!

Regards,

knocte

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Because you are using regular expressions! You are replacing any occurrance
of any character (".") with a comma.

At least read the manual on what the functions are :(

http://www.php.net/manual/es/function.ereg-replace.php
http://www.php.net/manual/es/function.str-replace.php

(spanish version, as I'm guessing that's your native language based on your
headers)

knocte wrote:
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; // displays "2.4"
Case 2:

$number = "2.4";
$number = ereg_replace(".",",",$number);
echo $number; // displays a strange thing!!!

Anyone know why ereg_replace() is not working? Thanks is advance, and
happy new year!

Regards,

knocte

Jul 17 '05 #2

P: n/a
Agelmar escribió:
Because you are using regular expressions! You are replacing any occurrance
of any character (".") with a comma.

At least read the manual on what the functions are :(

http://www.php.net/manual/es/function.ereg-replace.php
http://www.php.net/manual/es/function.str-replace.php

(spanish version, as I'm guessing that's your native language based on your
headers)


Oh thanks! What a dumb question I did!

Now I realize I have other functions using ereg_replace. In these cases,
do you think I should change it also to str_replace?:

function TA2DB($sTA){
$sDB = ereg_replace("\r\n","\\\\r\\\\n",$sTA);
$sDB= ereg_replace("\r","\\\\r\\\\n",$sDB);
$sDB= ereg_replace("\n","\\\\r\\\\n",$sDB);

return $sDB;
};

function DB2TA($sDB){
$sTA = ereg_replace("\\\\r\\\\n","\r\n",$sDB);

return $sTA;
};
Thanks in advance.

Jul 17 '05 #3

P: n/a
knocte wrote:
Agelmar escribió:
Because you are using regular expressions! You are replacing any
occurrance of any character (".") with a comma.

At least read the manual on what the functions are :(

http://www.php.net/manual/es/function.ereg-replace.php
http://www.php.net/manual/es/function.str-replace.php

(spanish version, as I'm guessing that's your native language based
on your headers)


Oh thanks! What a dumb question I did!

Now I realize I have other functions using ereg_replace. In these
cases,
do you think I should change it also to str_replace?:

function TA2DB($sTA){
$sDB = ereg_replace("\r\n","\\\\r\\\\n",$sTA);
$sDB= ereg_replace("\r","\\\\r\\\\n",$sDB);
$sDB= ereg_replace("\n","\\\\r\\\\n",$sDB);

return $sDB;
};

function DB2TA($sDB){
$sTA = ereg_replace("\\\\r\\\\n","\r\n",$sDB);

return $sTA;
};
Thanks in advance.


Most definitely. ereg_ and preg_ do regular expression matching, and are
much slower than str_ functions, which simply match strings. Because you are
not using any regular expressions, str_replace would be a much better choice
than ereg_replace. You would use ereg_replace for things like
"<!--([^-]*([^-]|-([^-]|-[^>])))*//-->" where you actually want to match
patterns, not just specific strings.
Jul 17 '05 #4

P: n/a
Agelmar escribió:
Most definitely. ereg_ and preg_ do regular expression matching, and are
much slower than str_ functions, which simply match strings. Because you are
not using any regular expressions, str_replace would be a much better choice
than ereg_replace. You would use ereg_replace for things like
"<!--([^-]*([^-]|-([^-]|-[^>])))*//-->" where you actually want to match
patterns, not just specific strings.


Thanks but, following your advice, I have changed ereg_replace by
str_replace in these functions that use \r\n and stuff like that, and it
doesn't work properly.

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.