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.
Related
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
I use a textarea to write comments on my website. The comment is saved in a SQLite DB.
My problem is when I try to retrieve my comment from the DB in order to replace every carriage return with <p> tags (before showing it to the user).
I've first try the nl2br function and it works fine, plenty of <br/> appears on my code.
Then I've try :
substr_count($article->texte, '\n');
substr_count($article->texte, '\r');
But the return result is always 0. It surprises me because I thought nl2br would replace \n and \r chars.
Did I miss something ?
mb_detect_encoding($article->texte); //returns UTF8
You need to understand that PHP interprets text inside single quotes literally, but expands what is inside double quotes; so you will get a different result if you do
substr_count($article->texte, "\n");
To answer your question, using nl2br is quickest, but if you really want to replace every occurrance of "\n" with "</p><p>" then do:
$content = str_replace("\n", '</p><p>', $content);
Expressions like \n and \r are evaluated only when in double quotes, so try "\n" and "\r"
In my database I have the following text:
for x in values:
print x
I want to print this code on my HTML page. It is printed by PHP to the HTML file as it is. But when HTML is displayed by a browser I, of course, do not see text in this form. I see the following:
for x in values: print x
I partially solved the problem by nl2br, I also use str_replace(' ',' ',$str). As a result I got:
for x in values:
print x
But I still need to shift print x to the right. I thought that I can solve the problem by str_replace('\t',' ',$str). But I found out that str_replace does not recognize the space before the print as '\t'. This space is also not recognized as just a space. In other words, I do not get any before the print.
Why? And how can the problem be solved?
Quote the text in double quotes, like this
str_replace("\t", ' ', $str);
PHP will interpret special characters in double quoted strings, while in single quoted strings, it will just leave the string, with the only exception of \'.
Old and deprecated answer:
Copy the tab character (" ") from notepad, your databasestring or this post, and add this code:
str_replace(' ',' ',$str);
(this is not four spaces, it is the tab character you copied from notepad)
You need to place \t in double quotes for it to be interpreted as a tab character. Single quoted strings aren't interpreted.
always use double quotes when using \t \n etc
It can be tricky because tabs don't actually have a fixed size and you'd have to calculate tab stops. It can be simpler if you print blank space as-is and instruct the browser to display it. You can use <pre> tags:
<pre>for x in values:
print x</pre>
... or set the white-space CSS property:
div.code{
white-space: pre-wrap
}
(As noted by others, '\t' is different from "\t" in PHP.)
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.
Well, I am abit confuse using these \r,\n,\t etc things. Because I read online (php.net), it seems like works, but i try it, here is my simple code:
<?php
$str = "My name is jingle \n\r";
$str2 = "I am a boy";
echo $str . $str2;
?>
But the outcome is "My name is jingle I am a boy"
Either I put the \r\n in the var or in the same line as echo, the outcome is the same. Anyone knows why?
Because you are outputting to a browser, you need to use a <br /> instead, otherwise wrap your output in <pre> tags.
Try:
<?php
$str = "My name is jingle <br />";
$str2 = "I am a boy";
echo $str . $str2;
?>
Or:
<?php
$str = "My name is jingle \n\r";
$str2 = "I am a boy";
echo '<pre>' .$str . $str2 . '</pre>';
?>
Browsers will not <pre>serve non-HTML formatting unless made explicit using <pre> - they are interested only in HTML.
Well in your example you've got \n\r rather than \r\n - that's rarely a good idea.
Where are you seeing this outcome? In a web browser? In the source of a page, still in a web browser? What operating system are you using? All of these make a difference.
Different operating systems use different line terminators, and HTML/XML doesn't care much about line breaking, in that the line breaks in the source just mean "whitespace" (so you'll get a space between words, but not necessarily a line break).
You could also use nl2br():
echo nl2br($str . $str2);
What this function does is replace newline characters in your string to <br>.
Also, you don't need \r, just \n.
In HTML, spaces, tabs, linefeeds and carriage returns are all equivalent white space characters.
In text, historically the following combinations have been used for newlines
\r on Apple Macs
\r\n on Windows
\n on Unix
Either use \n (*NIX) or \r\n (DOS / Windows), \n\r is very uncommon. Once you fix that, it should work just fine.
Of course, if you're outputting HTML, a line break does nothing unless it's inside <pre></pre> tags. Use <br /> to separate lines in HTML. The nl2br() function can help you to convert line breaks to HTML if needed.
Also, if you use single-quoted strings (your example has double quoted strings), \r and \n will not work. The only escape characters available in single quoted strings are \' and \.
Are you displaying the results in an HTML page? if so, HTML strips whitespace like newlines. You'd have to something like use '<br />' instead of '/r/n' in HTML.