I am trying to remove the escape sequences present in between my output. I am coding using php and I have output from the function and it contains escape sequences like \r and \n. How do I escape these stuff from output so that I get a proper output. The issue is that I am trying to add this output to a csv file, so it will take \n as next line.
Single quotes inhibit escape sequences. '\r' is two characters, \ and r; "\r" is CR.
Use str_replace function:
$your_string = "here is your string"
$your_string = str_replace("\n", "", $your_string);
$your_string = str_replace("\r", "", $your_string);
You need to use stripslashes and str_replace check them out Here and Here
This post indicates that keeping the newline in the field should work, so long as:
newlines inside cells are represented as \n (str_replace("\r\n", "\n", $cell))
cells are quoted
rows are terminated with \r\n
If you're not already I'd recommend using fputcsv(), as it will take care of the quoting for you.
Related
I want to remove all \r \n \r\n which is pretty easy to so I wrote:
str_replace(array("\r","\n"),"",$text);
but I saw this line:
str_replace(array("\r","\n","\\r","\\n"),"",$text);
and I was wondering what is the double backslash means \\r and \\n.
\ is an escape character, it's used to escape the following character.
In "\n", the backslash escapes n and the result will be a new line character.
In "\\n", the first backslash escapes the second backslash and the n is kept as is, so the result is a string containing \n (literally).
See the PHP official documentation > Strings.
In the context of your question, str_replace() will remove new lines ("\n" and "\r") and also remove \n and \r from the string ("\\n" and "\\r" respectively). There's no reason a text contains the words \n and \r, so it seems that using "\\n" and "\\r" has no interest here.
The first backslash escapes the second one, so it matches a literal backslash in $text.
I'm not sure why you would want to match that if you just want to remove newlines and carriage returns from the string.
I have a string coming from a language file containing strings with the text in the current language.
$str = 'blabla\n\nmore blabla';
$str is going to be used in an textarea where the \n must be a linebreak
If I place it inside double quotes this works.
The problem is that $str will always be in single quotes. I've been Googling and searching this site. There are many similar questions, but I didn't manage to find a solution.
How can I convert my single-quoted string (with a literal "\n") to a doublequoted string (where "\n" is converted to a linebreak)?
$str = str_replace('\n', "\n", $str);
I have this code for saving line breaks in text area input to database:
$input = preg_replace("/(\n)+/m", '\n', $input);
On examining the input in the database, the line breaks are actually saved.
But the problem is when I want to echo it out, the line breaks do not appear
in the output, how do I preserve the line breaks in input and also echo them
out. I don't want to use <pre></pre>.
You are replacing actual sequences of newlines in the $input with the literal two character sequence \n (backslash + n) - not a newline. These need to be converted back to newlines when read from the database. Although I suspect you intend to keep these actual newlines in the database and should be using a double quoted string instead...
$input = preg_replace('/(\n)+/m', "\n", $input);
Note that I have replaced the first string delimiters with single quotes and the second string with double quotes. \n is a special sequence in a regular expression to indicate a newline (ASCII 10), but it is also a character escape in PHP to indicate the same - the two can sometimes conflict.
I think PHP has the solution:
nl2br — Inserts HTML line breaks before all newlines in a string
Edit: You may need to replace CR / LF to make it work properly.
// Convert line endings to Unix style (NL)
$input = str_replace(array("\r\n", "\r"), "\n", $input);
// Remove multiple lines
$input = preg_replace("/(\n)+/m", '\n', $input);
// Print formatted text
echo nl2br($input);
i get from db a text like this.
{br}{/br}hello!{br}{/br}
this text is outputted inside a textarea element.
what i need is to replace all the '{br}{/br}' with invisible char '\n' which should set a enter space in the textarea itself. hoping :)
what i tryed to do is.
$text = str_replace('{br}','\n',$text);
$text = str_replace('{/br}','\n',$text);
then output $text in textarea, but chars \n are visible :|
Use str_replace with a double quoted "\n" for it to be interpreted as a newline; '\n' with single quotes is a literal backslash followed by an n.
$text = str_replace('{br}{/br}', "\n", $text);
I'm not sure why you're calling str_replace once for {br} and once for {/br}. Do you want each pair of {br}{/br} to be replaced by two new lines? If so, you could do that more simply with a single call:
$text = str_replace('{br}{/br}', "\n\n", $text);
You need to put the \n in double quotes, not single quotes. Variables and escape sequences are not interpolated in single quotes. Also, you probably want to replace the whole string {br}{/br} with a single new line - with what you have done you will replace it with two.
So:
$text = str_replace('{br}{/br}',"\n",$text);
Is probably what you want. It's probably worth you reading this so you know what you can/can't do with strings in PHP.
Try using double quotes
$text = str_replace('{/br}', "\n", $text);
How can characters " \n \t \r " be replaced with '-' ?
echo preg_replace('/\s/','-','\n\t\n\r\n');//output '\n\t\n\r\n' instead should be'-----'
Edit: I have dynamic content in real app like:
preg_replace('/\s/','-',$_Request['content']);
can I fix it by adding "" around variable?
preg_replace('/\s/','-',"$_Request['content']");
Edit2:
How can be string converted from format 'str' to format "str"?
Thanks
Well, two things. First, the problem is single quotes in your replacement string. Meta-Characters (\n\t\r, etc) are not processed inside of single quotes.
However, don't use a regex for this. There's no need for the complexity of the regex. Use
Either use str_replace:
echo str_replace(array("\r", "\n", "\t", "\v"), '-', "\r\n\t\r\v\n\t");
Or strtr:
echo strtr("\r\n\t\r\v\n\t", "\r\n\t\v", '----');
Edit: Ahh, now I see what you're getting at. You have a string with a literal \r\n\t\r\v\n\t in it, and want to replace them out. Well, you can do that via regex:
$regex = '/(\s|\\\\[rntv]{1})/';
$string = preg_replace($regex, '-', $_GET['content']);
Basically, it matches any space character, and any literal \ followed by either r, n, t or v...
If you are looking to replace the actual whitespace characters, you need to enclose the input string in double quotes (") so PHP converts the escape sequences for you:
echo preg_replace('/\s/', '-', "\n\t\n\r\n");
Else if the escape sequences occur literally (i.e. you see \n\t\n\r\n instead of line feed, tab, line feed, carriage return, line feed), you need to replace by the following character class (and keep single quotes (') on the input string):
echo preg_replace('/\\\\[rnt]/', '-', '\n\t\n\r\n');
You ought to be passing content through $_POST instead of $_GET, I don't know how PHP handles tabs, newlines and returns in GET variables.
You are using 's instead of "s. You should change your code to:
echo preg_replace('/\s/','-',"\n\t\n\r\n");
See here: single-quoted and double-quoted.
http://www.php.net/manual/en/language.types.string.php
There's also a string method for that:
echo strtr($str, "\r\n\t\v ", "-----");
If you want to remove linebreaks but retain spaces, then remove the trailing and the fifth -.
Since you seemingly want literal \r and \n converted, you need to use a map (or even a regex) like:
echo strtr($str, array('\\r'=>"\r", '\\n'=>"\n", '\t'=>"\t", ' '=>"␣"));
// single quoted strings escaped twice for illustration
Try:
echo preg_replace('/\s/','-',"\n\t\n\r\n");
Note the double quotes on the string.
If you enclose a string with single quotes, special characters lose their special meaning:
echo preg_replace('/\s/','-',"\n\t\n\r\n");