You can remove it by replacing it with nothing,
str_replace("\n","",$row['name']);
If you need your line break to be in your JS string you can double escape it,
str_replace("\n","\\n",$row['name']);
Answer from lostsource on Stack OverflowYou can remove it by replacing it with nothing,
str_replace("\n","",$row['name']);
If you need your line break to be in your JS string you can double escape it,
str_replace("\n","\\n",$row['name']);
I have the same issue like this before. json_encode in PHP and JSON.parse() in JS helped me resolve this. In your case, I would use this code:
<td onclick="openFile('<?php echo htmlentities ($row['name'])?>','
<?php echo json_encode($row['content'])?>')">
<a href="#nf" data-toggle="tab"><?php echo $row['name']?></a></td>
Then on your openFile() function, that is where Im gonna parse content parameter using JSON.parse() like this:
function openFile(name, content){
content = JSON.parse(content);
....
// your codes here
}
This is a bit confusing
is there any php method to remove new line char from string?
It looks like you actually want them replaced with a space.
$str = str_replace(array("\r\n", "\n", "\r"), ' ', $str);
Assuming the replacing goes from left to right, this should suit Windows text files.
The first grouping is to match Windows newlines which use both \r and \n.
There is no way to escape newline in PHP.
As mentioned in PHP documentation on strings after listing all escaped characters:
As in single quoted strings, escaping any other character will result in the backslash being printed too.
So you can do it by breaking your string in each line and concatenating them by dot like this:
$str = "Hi " .
"there" .
"!";