For some reason my server is ignoring "\t" -- yes I am using double quotes.
When I echo out a string like "abc\tdef\tghi" the "\t" is shown as a small "HT" with the "H" slightly smaller and above the "T"
Oddly, "\n" works and gives me a new line.
Not sure if this is an issue with PHP, NGINX, or something else, any ideas?
Try to use the the escape character instead the escape sequence. This works like a charm and you don't neet to worry about anything.
Let's say you have something like this:
$text = "First\tSecond\tThird\t...";
echo $text;
Replace it with this:
$text = "First\x09Second\x09Third\x09...";
echo $text;
Please note that using 'single quotes' with escape characters will get you '\x09' instead a real tabulator!
A list of all escape characters can be found in php.net manual:
PHP.NET escape characters
Related
I found that in the muilti-module which is created by phalcon-tool has these code:
$application = new Application($di);
echo str_replace(["\n","\r","\t"], '', $application->handle()->getContent());
Why remove "\n","\r","\t"?
As Spangen pointed out, these are the escape sequences for some special 'whitespace' characters.
This link here has more information on them: http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Now, an important note: for them to work as intended in PHP, you need to enclose them in double quotes, like: "\t", "\n". Single quotes won't produce the intended effects of these escape sequences: '\t', '\n'.
To illustrate this more, you can run this code and see the results. Running it from console like php myfile.php might cause some visual glitches, and running it in the browser will need that you view the 'source code'.
echo "Let's test... ";
echo "Because no new line characters were added, this sentence will be printed in the same line as the previous phrase.";
echo "\n";
echo "But now a new line was added, by typing \\n enclosed in double quotes.";
echo "\n";
echo "Now, let's add a tab, between the next two words: hello \t there.";
echo "\n";
echo "Now, let's add a carriage return, which will 'force' the 'cursor' in this string to move to the beginning, thus 'splitting' this string into two. Adding it now: \r There, I just added it before this last sentence.";
echo "\n";
echo 'Finally, these special characters will not work as intended if we just enclose the string with single quotes, as done in this string: \n \t \r';
This link here has more information on the difference between new lines and carriage returns: https://stackoverflow.com/a/12747850/466395
So yeah, in the original code that you posted, the person removed those special characters using str_replace(), because they tend to produce 'visual glitches' (unintented malformed output) in console.
This is to replace newlines, line feeds and tabs with a single space so that the application's details can be written to STD_OUT as a single line with no wrapping.
For example I want to store the String "That's all". MySQL automatically escapes the ' character. How do I echo that String from the database using php but remove the \ in front of escaped characters like \' ? I would also like to preserve other formatting like new lines and blank spaces.
Have you tried stripslashes(), regarding the linebreaks just use the nl2br() function.
Example:
$yourString = "That\'s all\n folks";
$yourString = stripslashes(nl2br($yourString));
echo $yourString;
Note: \\ double slashes will turn to \ single slashes
You should probably setup your own function, something like:
$yourString = "That\'s all\n folks";
function escapeString($string) {
return stripslashes(nl2br($string));
}
echo escapeString($yourString);
There are also several good examples in the nl2br() docs
Edit 2
The reason your are seeing these is because mysql is escaping line breaks, etc. I am guessing you are using mysql_* functions. You should probably look into mysqli or PDO.
Here is an example:
$yourString = "That's all
folks";
echo mysql_escape_string($yourString);
Outputs:
That\'s all\r\n folks
If you use prepared statements, those characters will not be escaped on insert.
Use stripslashes() to remove slashes if you cannot avoid adding slashes on input.
At first, magic_quotes_gpc escapes the character like ' or ". You can also disable this in your php.ini. But then you should escape the things yourself that no query can get "infected".
Lookup mysql injection for more information.
When the escaped string is been written in your database. The string doesn't contain theses escape charakters and when you output them again. You should see the result as you want it.
Me for myself prefer the method by storing everything without escapes and escape or display things when I output them. You could also easily use an str_replace("\n", "", $text) to prevent newslines are displayed.
Greetings MRu
PHP is echoing JavaScript (I'm using the jQuery library) something like this:
echo 'var users = $("#add").val().split("\n");';
However, the \n is creating a line break in what the echoed script looks like, and therefore breaking the JavaScript. Is there a way to circumvent this?
Many thanks!
The \n is an escape sequence meaning newline. Backslashes are the beginning of escape sequences, to output a backslash then write \\. So you want \\n. Other useful escape sequences include the quote: use \" to put a quote into the string instead of ending the string.
echo "var users = $(\"#add\").val().split(\"\\n\");";
Not sure If you looking for this
echo "<script>alert('Line1\\\\nThis still in Line1')</script>";
On my home page, I display some info about the hikes (it is a hiking site) and embarassinlgy,
the \r\n characters still show up.
I do these functons
$hike_description = htmlspecialchars ($hike_description);
$hike_description = nl2br($hike_description);
And still those characters do not go away. You can take a look yourself at http://www.comehike.com
Would you know what is the proper way to get rid of the \r\n characters? You can see it happening in the "Upcoming Hikes" section...on the 3rd hike.
Thanks!
In your case, the following will work:
$hike_description = str_replace ( "\\r\\n","<br />", $hike_description);
The text \r\n is literal, rather than control characters.
Try manually replacing the sequence
str_replace( "\r\n", "<br />", $hike_description );
The \n\r in your page are not escape sequences... They are actually a \ character followed by a n character followed by a \ character followed by a r character.
In your database, you should store that as the actual characters, not the escape sequences. Then, calling nl2br() will work as expected.
Yes, you can do a str_replace(), however, you should instead fix the encoding of your data in your database. It will save you trouble in the future.
I have a php string with a lot of information to be displayed inside a textarea html element.
I don't have access to that textarea nor to the script (if any) that generates it.
$somestring = 'first line \nSecond line \nThird line.';
$somestring as NOT been "worked" with trim or filter_var. Nothing.
On the textfield, I get the \n printed on the textarea hence, not interpreted.
What can I try in order to have those new lines applied?
Thanks in advance.
Try wrapping $somestring with " (double quotes) instead of ' (single quotes)
\n, \r and other backslash escape characters only works in double quotes and heredoc. In single quotes and nowdoc (the single quote version of heredoc), they are read as literal \n and \r.
Example:
<?php
echo "Hello\nWorld"; // Two lines: 'Hello' and 'World'
echo 'Hello\nWorld'; // One line: literally 'Hello\nWorld'
echo <<<HEREDOC
Hello\nWorld
HEREDOC; // Same as "Hello\nWorld"
echo <<<'NOWDOC'
Hello\nWorld
NOWDOC; // Same as 'Hello\nWorld' - only works in PHP 5.3.0+
Read more about this behaviour in the PHP manual
EDIT:
The reason single and double quotes behave differently is because they are both needed in different situations.
For instance, if you would have a string with a lot of new lines, you would use double quotes:
echo "This\nstring\nhas\na\nlot\nof\nlines\n";
But if you would use a string with a lot of backslashes, such as a file name (on Windows) or a regular expression, you would use single quotes to simplify it and avoid having unexpected problems by forgetting to escape a backslash:
echo "C:\this\will\not\work"; // Prints a tab instead of \t and a newline instead of \n
echo 'C:\this\would\work'; // Prints the expected string
echo '/regular expression/'; // Best way to write a regular expression
$somestring = "first line \nSecond line \nThird line.";
http://php.net/types.string <-- extremely useful reading
this article is a cornerstone of PHP knowledge and it's just impossible to use PHP without it.
unlike most of manual pages which are are just for quick reference, this very page is one which every developer should learn by heart.