ereg_replace(): this drives me nuts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • knocte

    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(", ",".",$numb er);
    echo $number; // displays "2.4"


    Case 2:

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

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

    Regards,

    knocte

  • Agelmar

    #2
    Re: ereg_replace(): this drives me nuts

    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 :(

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.



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

    knocte wrote:[color=blue]
    > 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(", ",".",$numb er);
    > echo $number; // displays "2.4"
    >
    >
    > Case 2:
    >
    > $number = "2.4";
    > $number = ereg_replace(". ",",",$numb er);
    > echo $number; // displays a strange thing!!!
    >
    > Anyone know why ereg_replace() is not working? Thanks is advance, and
    > happy new year!
    >
    > Regards,
    >
    > knocte[/color]


    Comment

    • knocte

      #3
      Re: ereg_replace(): this drives me nuts

      Agelmar escribió:[color=blue]
      > 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)[/color]

      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.

      Comment

      • Agelmar

        #4
        Re: ereg_replace(): this drives me nuts

        knocte wrote:[color=blue]
        > Agelmar escribió:[color=green]
        >> 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)[/color]
        >
        > 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.[/color]

        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.


        Comment

        • knocte

          #5
          Re: ereg_replace(): this drives me nuts

          Agelmar escribió:[color=blue]
          > 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.
          >[/color]

          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.

          Comment

          Working...