Reputation: 774
I want escape string PHP_EOL
or double quote "\n"
selected from db
I tried:
<?php
header("Content-Type: text/plain");
$str = 'Hello\nWorld!';
echo $str;
sqlInsert("insert ... (str) values ('{$str}')");
Output worked
Hello\nWorld!
But when I want get it from database:
$str2 = sqlget("select ... str");
echo ''.$str2;
Will be: not working
Hello
World!
What's Problem!
Upvotes: 1
Views: 412
Reputation: 19780
SQL doesn't works as PHP with EOL in single or double quoted strings.
You could either / or :
1. Store backslash in your database :
You need to escape the backslashes :
$str = 'Hello\nWorld!';
echo $str ;
$str = str_replace('\\n','\\\\n',$str);
sqlInsert("insert ... (str) values ('{$str}')");
2. Transform the output :
Replace newlines by litteral \n
:
$str = get_from_sql(...);
$str = str_replace("\n", '\n', $str);
Upvotes: 1