-1

I have a question, what is the most optimal way for replace the strings for example, I want to replace \n and \r\n with ''. I have 2 options :

  1. Replaces nested:

    SELECT REPLACE(REPLACE(m3.old_message,'\n',''),'\r\n','')

  2. Preg replace:

    if ($aField== 'user') { $sValue = preg_replace... }

Thx in advance.

hakre
  • 193,403
  • 52
  • 435
  • 836
Harea Costea
  • 275
  • 5
  • 19
  • Both ways can be optimal. Before finding the optimal way you should have a benchmark mechanism. Firstly find your execution bottleneck(http://en.wikipedia.org/wiki/Program_optimization#Bottlenecks). – Plamen Nikolov Jan 21 '15 at 09:29
  • Why won't u check your methods on test data similiar to one you are using? – Whencesoever Jan 21 '15 at 09:37
  • 1
    probably your 'outer' replace will never fire, as \n is already replaced there... – thriqon Jan 21 '15 at 10:01

1 Answers1

1

In PHP you can do:

$string = preg_replace('/\R/', '.', $string);

Where \R stands for any line break, \r or \n or \r\n.

Toto
  • 89,455
  • 62
  • 89
  • 125