1

I'm using PHP in order to insert data in an sqlite database.

I have a string like the following:

$string =  "Good morning!<br />It is a wonderful day!<br />"

I would like to replace all occurrences of <br /> with \n.

I did the following in PHP in order to replace the <br /> but it did not work:

<?php
    $string =  "Good morning!<br />It is a wonderful day!<br />"
    $symbols =array('<br />');
    $replace_symbols = array('\\n');
    $string = str_replace($symbols, $replace_symbols,$string);
    //InsertIntoSqlite($db,$string);
?>

When I connect to the sqlite database with SQLITE manager, I see the following inserted in the database:

sqlite: Good morning!\nIt is a wonderful day!\n

What can I do in order to fix that?

SOLVED:

I did the following:

<?php

    $string = str_replace("<br />","\n",$string);

 ?>
programmer
  • 4,571
  • 13
  • 49
  • 59

1 Answers1

2

'-quoted strings do not honor ANY escapes, except the escape itself. '\t' is not at tab character, it's a \ followed by a t. You want "\n" instead, which is an actual newline.

echo '\\ \t \n \r'; // spits out: \ \t \n \r
echo "\\ \t \n \r"; // spits out \ [tab] [newline] [carriagereturn]
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • So, do I need to use double quotes instead of single quotes? – programmer Sep 11 '14 at 16:32
  • Yes, and only one backslash. If you just blindly change your code to `"\\n"`, you'll STILL get `\n` in your output, because ``\\`` will be parsed first and reduced to just ``\``, leaving you with a ``\`` and an `n` char in your string, not a newline char. – Marc B Sep 11 '14 at 16:44
  • Thanks for that, It worked for me! I can see the new line in sqlite now! – programmer Sep 11 '14 at 16:49
  • Hi, I have the same problem with " when I do: `$string = str_replace(""","'",$string);` OR `$string = str_replace(""","\'",$string);` I get the following error in PHP:Warning: SQLite3::exec(): near "the" can not insert, What can I do in order to fix that? Thanks – programmer Sep 11 '14 at 19:01