I"m trying to echo a text into one line with no line breaks. I've tried to use nl2br and than replace all /n or /r with nothing. I want the output to be:
"I"m trying to write my text in one line"
instead of:
"I"m trying to write my text
in one line"
<!DOCTYPE html>
<html>
<body>
<?php
$mytext = 'I"m trying to write my text
in one line';
$mytext = nl2br($mytext);
$mytext = preg_replace('/\r?\n|\r/','', $mytext);
$mytext = str_replace(array("\r\n","\r","\n"),"", $mytext);
echo $mytext;
?>
</body>
</html>
Other than not using
nl2br, and needing to replace with a space, not an empty string, your code should work fine (note you can optimise thepreg_replaceby using\Rwhich matches any unicode newline sequence):Output:
Now if you want to make sure it remains on one line in the HTML, you need to replace spaces with non-breaking spaces e.g.
Here's a demo using
in JS: