-1

I need help for replacing line breaks in my mysql database with \n. If I copy mysql text and past in MS Word, I have this:

image 1

I need to replace the break lines with \n. Like this:

image 2

There are any MySql command for this and one PHP or Javascript function?

Thank You.

Blu
  • 4,036
  • 6
  • 38
  • 65
ptCoder
  • 2,229
  • 3
  • 24
  • 38

4 Answers4

2

In Php there is a function named nl2br

 echo nl2br("hello\nWorld");

and the result is

 hello<br/>World

In sql there is a function named REPLACE

   UPDATE yourtable SET field=REPLACE(REPLACE(field, CHAR(13), ''), CHAR(10), '')

And Finally in javaScript you have replace too

myString = myString.replace(/\r?\n/g, "");

Good Luck

Bellash
  • 7,560
  • 6
  • 53
  • 86
1

If MS Word is parsing the text with linebreaks, it means it already has them.

Use a text editor like notepad++ and set it to show all characters. You will see what special chars are being used as linebreaks (it might be \n, \r and a mix of both).

They won't show up when you browse your database with phpmyadmin, because its textarea control will parse the linebreaks for you.

ffflabs
  • 17,166
  • 5
  • 51
  • 77
  • Thank you but I don’t need replace ‘\n’ to html ‘
    ’. I just need to replace line breaks with ‘\n’ character.
    – ptCoder Apr 01 '14 at 11:59
1

I am not sure if I understand your input string correctly, but if, the code would look like this.

  function replace_lb($str){
    return str_replace(array(chr(10), chr(13)), '\n', $str );
  }

Usage:

  $query = mysql_query('SELECT `id`,`value` FROM table ', $link);
  while ($result = mysql_fetch_array($query)){
    mysql_query('UPDATE table SET `value`= "'.replace_lb($result['value']).'" WHERE id="'.$result['id'].'"', $link);
  }

The code could use some optimalization, but I think it will suit your needs

Adam Fischer
  • 1,075
  • 11
  • 23
0

try the php nl2br

<?php
$result = nl2br($row['MYSQL_RESULT']);  // from the return from your query
?>
  • Thank you but I don’t need replace ‘\n’ to html ‘
    ’. I just need to replace line breaks with ‘\n’ character.
    – ptCoder Apr 01 '14 at 11:59