Hi,
Like in the subject. I would like to have one text line insted of couples, when displaying field called "remarks". I have to somehow delete LF and CR characters and replace them by "space" character. For someone who knows php very well it's a piece of cake, but I don't know how to do it. Please help !

Martin

    3 years later
    qwas0987;10732924 wrote:

    Hi,
    Like in the subject. I would like to have one text line insted of couples, when displaying field called "remarks". I have to somehow delete LF and CR characters and replace them by "space" character. For someone who knows php very well it's a piece of cake, but I don't know how to do it. Please help !

    Martin

    Hi, I stumbled across this forum and decided to complement the answer:

    I prefer to use ereg_replace so I can remove CR and LF in one line:

    $string = ereg_replace("[\r\n]", '', $string);
      claudioimai;10899434 wrote:

      Hi, I stumbled across this forum and decided to complement the answer:

      I prefer to use ereg_replace so I can remove CR and LF in one line:

      $string = ereg_replace("[\r\n]", '', $string);

      If you do use regex instead (in this case, str_replace, as someone already mentioned, would suffice), it is not advisable to use ereg, as this will not be included within the PHP core as of version 6. POSIX is being dropped in favor of PCRE (Perl Compatible Regular Expressions- which preg is a part of). So I would recommend learning preg instead.. otherwise, you run the risk of having your code break once your hosting provider switches to PHP 6 (when it launches) and not have the POSIX extension installed. Future-proof yourself now...

      $string = preg_replace('#[\r\n]#', '', $string);
      

        Hi.

        I just pointed that because it looked simpler to use a regex in this case (less code).

        I actually didn't know about preg x ereg.

        Thanks!

          claudioimai;10899454 wrote:

          Hi.
          I just pointed that because it looked simpler to use a regex in this case (less code).

          Less code does not necessarily = faster. It is possible to have less code bloat, but have those code components be heavier in execution times.

          Functions like str_replace tend to be faster than their preg / ereg replace counter parts, even with about the same amount of code.

          Consider the test results of the following:

          $str = 'I am Mr.X';
          echo 'String: "' . $str . '"<br /><br />';
          $loop = 10000;
          
          $time_start = microtime(true);
          for($i = 0; $i < $loop; $i++){
             $result = str_replace('X', 'Y', $str);
          }
          $time_end = microtime(true);
          $elapsed_time = round($time_end-$time_start, 4);
          echo 'Method: str_replace(\'X\', \'Y\', $str)<br />Time: ' . $elapsed_time . '<br />Result: "' . $result . '"<br /><br />';
          
          $time_start = microtime(true);
          for($i = 0; $i < $loop; $i++){
             $result = preg_replace('#X#', 'Y', $str);
          }
          $time_end = microtime(true);
          $elapsed_time = round($time_end-$time_start, 4);
          echo 'Method: preg_replace(\'#X#\', \'Y\', $str)<br />Time: ' . $elapsed_time . '<br />Result: "' . $result . '"<br /><br />';
          
          $time_start = microtime(true);
          for($i = 0; $i < $loop; $i++){
             $result = ereg_replace('X', 'Y', $str);
          }
          $time_end = microtime(true);
          $elapsed_time = round($time_end-$time_start, 4);
          echo 'Method: ereg_replace(\'X\', \'Y\', $str)<br />Time: ' . $elapsed_time . '<br />Result: "' . $result . '"<br /><br />';
          

          Sample output:

          String: "I am Mr.X"
          
          Method: str_replace('X', 'Y', $str)
          Time: 0.0277
          Result: "I am Mr.Y"
          
          Method: preg_replace('#X#', 'Y', $str)
          Time: 0.08
          Result: "I am Mr.Y"
          
          Method: ereg_replace('X', 'Y', $str)
          Time: 0.105
          Result: "I am Mr.Y"
          

          Now in a single pass, the speed difference between the these would be practically inperceptable... but in this case, I increased the loop to 10,000 times..as you can see, str_replace is faster, yet all functions output the same thing (with about the same amount of code).

            Besides that, I don't see how

            $string = ereg_replace("[\r\n]", '', $string);

            is "less code" than

            $string = str_replace(array("\r", "\n"), ' ', $string);

              However, the regexp approach is a bit more powerful if, for instance, you want to collapse multiple consecutive matches into one space:

              $string = preg_replace('/[\r\n]+/', ' ', $string);
              

                Yeah, I failed to mention in my response what NogDog is saying...the trade off of speed with flexibility.. str_replace cannot perform complex regex pattern replacements, where as preg can. So it basically boils down to knowing what you need to change.. if it can be done with str_replace instead, it is the better choice to use as far as speed if concerned..

                It can be debated however with regards to how much reliance goes into preg.. while there may be some instances that can be done entirely within preg, sometimes, using less preg in conjunction with separate (and much faster) non regex functionality can work faster, yet co-operatively maintain the needed flexibility.

                  Write a Reply...