473,322 Members | 1,734 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,322 software developers and data experts.

ereg_replace(): this drives me nuts

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
4 2408
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
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
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
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 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...
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 :-) ...
6
by: chromis | last post by:
Hi, I'm trying to remove line breaks from a user-inputted string and trying to do this using ereg_replace and a regular expression but struggling, hope someone can help! I would like to make...
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:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.