I can't seem to figure out why the following code doesn't produce a new line in my text file - neither does using \n etc either - any ideas what could be wrong?
$data = $name . ' | ' . $_POST['comment'] . PHP_EOL;
//write to file
$f = file_put_contents('posts.txt', $data, FILE_APPEND);
Are you viewing the text file in an internet browser by any chance?
If you are, the browser will get rid of the newline characters (unless you're using PRE tags).
Try double quotes: $data = $name . ' | ' . $_POST['comment'] . "\n";
Or: $data = "$name | {$_POST['comment']}\n";
Have you tried \r or \n\r ? Just an idea.
Related
I try to print a big JSON block (100k) to the browser, but the server fails without an error.
For example:
echo 'var config = ' . json_encode( $config ) . ';' . PHP_EOL;
I Have found that if i send a small piece, it's OK.
I have found that if I put line breaks in the JSON string, it's OK even if the string is 400k.
For example:
$config_json = json_encode( $config );
$config_json = str_replace( '},', '},' . PHP_EOL, $config_json );
echo 'var config = ' . $config_json . ';' . PHP_EOL;
But the breaklines breaks my JSON.
So, if it's a buffer setting, why the PHP_EOL helps?
I have tried also to split the JSON to pieces like here: https://stackoverflow.com/a/19156563/1009525, But without success, Only the breaklines helps me.
As you write
the server fails without an error
I presume you mean that the server sends a response to the client (status code: 200 - no error), but the response body (the content) is empty (this is the failure).
You should check this because if actually the server sends a response with content then the issue is not with php, nginx or buffering.
Otherwise (as suggested in comments) maybe the JSON instead of inside a <script> - </script> block may be wrapped between <pre> tags and this could be the problem (but I can't help unless you post more of your code).
From now on I assume the response sent from the server is empty
The code you posted is valid and is supposed to handle correctly the output string you're building up (that's far below PHP limits).
Said that it seems a weird buffering issue. I write "weird" because as far as I know (and I took time to do some research too) buffering should not be influenced by line breaks.
I have found that if I put line breaks in the JSON string, it's OK even if the string is 400k.
A quick workaround to solve your problem is to output a valid JSON with line breaks. You just need to specify an option to json_encode:
echo 'var config = ' . json_encode( $config, JSON_PRETTY_PRINT ) . ';' . PHP_EOL;
JSON_PRETTY_PRINT tells json_encode to format the json to be more readable and doing so will add line breaks.
(Note that this option is available for PHP 5.4.0 and above)
I hope the above solution works for you.
Anyway I strongly suggest you to investigate further the issue in order to let the original code too to work.
First you should ensure you're running a recent and stable version of both nginx and php.
Then I would check nginx configuration file, php-fpm configuration (if you're using php-fpm) and finally php configuration.
Also check php, nginx, and php-fpm error logs.
try using php heredoc for echoing http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
In case you don't have PHP version > 5.4.0 installed on your server a quick workaround could be something like this. The below snippet works for a test array. Initial test was with an array of 250Kb. Since i can't post the actual test array here is a test link with a smaller example. It is as the result of JSON_PRETTY_PRINT though.
$out = json_encode($arr,JSON_FORCE_OBJECT);
$out = str_replace( ':{', ':' . PHP_EOL . ' ' . '{', $out );
$out = str_replace( '},', PHP_EOL . ' },', $out );
$out = str_replace( ',', ',' . PHP_EOL . ' ', $out );
$out = str_replace( '},' . PHP_EOL . ' ', '},' . PHP_EOL . ' ', $out );
$out = str_replace( '}}', PHP_EOL . ' }' . PHP_EOL . '}', $out );
echo $out;
I am currently developing a website with a list of names. Some of the names include apostrophes ' and I want to link them to a website using their name.
I want to link to the a url like:
example.com/ (their name)
And by doing that, I first replace " " with "+". So the links looks like: example.com/john+doe
But if the name is John'Doe it turns the url into just example.com/john
And skips the lastname.
How can I fix this? I tried changing ', \' etc, to html codes, to ', and more, but nothing seems to work.
Here is my current code:
$name = $row['name'];
$new_name = str_replace(
array("'", "'"),
array(" ", "+"),
$name
);
echo "<td>" . $name . " <a href='http://www.example.com/name=" . $new_name . "' target='_blank'></a>" . "</td>";
What I want it to look like:
John Doe Johnson ----> http://www.example.com/name=John+Doe+Johnson
John'Doe Johnson ----> http://www.example.com/name=John'Doe+Johnson
It changes the spaces to +, but how can I fix the apostrophes? Anyone knows?
You should be using PHP's function urlencode, php.net/manual/en/function.urlencode.php.
<?php
$name = $row['name'];
//$urlname = urlencode('John\'Doe Johnson');
$urlname = urlencode($name);
echo "<td>$name<a href='http://www.example.com/name=$urlname' target='_blank'>$name</a></td>";
Output:
<td>John%27Doe+Johnson <a href='http://www.example.com/name=John%27Doe+Johnson' target='_blank'></a></td>
echo urlencode("John'Doe Johnson");
return
John%27Doe+Johnson
I'm making this chat server, but it doesn't work quite well. When you send a piece of text, it first gets encoded by the function base64_encode() and then gets sent to a MySQL database.
Then the receiver gets the text from that same MySQL database, which is of course first decoded by the function base64_decode().
The only problem is with the special characters like \n \' and \t: when I get the data from the database and print it between two textarea tags, I see \n as a string, and not as actual line breaks.
In short, I need to fix this problem:
$String = 'Line 1 \n Line 2';
print '<textarea>' . $String . '</textarea>';
//The result I want
//<textarea> Line 1
//Line 2 </textarea>
The function nl2br doesn't work, because tags inside a textarea tag won't work, and also because there other characters like apostrophes.
Could anybody help me?
Thanks!
You need to enclose your string into double quotes, for special characters to be evaluated.
$String = "Line 1 \n Line 2";
print '<textarea>' . $String . '</textarea>';
If you change this:
$String = 'Line 1 \n Line 2';
print '<textarea>' . $String . '</textarea>';
to this:
$String = "Line 1 \n Line 2"; // double quote
print '<textarea>' . $String . '</textarea>';
... you will get the output you want.
This one is also works same as using " ... ", however maybe helps in your case:
$string = <<<EOT
Line 1 \n Line 2
EOT;
echo '<textarea>' . $string . '</textarea>';
As the others said, your problem is Single-Quotes.
I am putting together a string that I will output to a .srt file:
while ($row = mysql_fetch_array($res)) {
$srt = $srt . $row['line_number'] . PHP_EOL;
$srt = $srt . str_replace(".", ",", $row['start']) . " --> " . str_replace(".", ",", $row['end']) .PHP_EOL ;
$srt = $srt . br2nl($row['text']) . PHP_EOL;
$srt = $srt . PHP_EOL;
}
But it seems like PHP_EOL isn't working, because my output is:
100:00:02,107 --> 00:00:05,810you sure
and doesn't have any newlines. I am trying to get my output to be:
1
00:00:02,107 --> 00:00:05,810
you sure
followed by a newline.
It works when testing through localhost on my computer. Could the PHP version on my host be missing support for PHP_EOL?
The PHP manual says the PHP_EOL constant was available since PHP 4.3.10 and PHP 5.0.2
PHP_EOL (string)
The correct 'End Of Line' symbol for this platform. Available since PHP 4.3.10 and PHP 5.0.2 - http://php.net/manual/en/reserved.constants.php
So test to see if it exists:
var_dump(PHP_EOL); // should output: string(1) " "
OR
var_dump(defined("PHP_EOL")); // should output if exists: bool(true)
and if it is not defined, just define it manually if you want
define("PHP_EOL", "\n");
OR just use echo "\n" or echo "\r\n"
The other possible reason is when you output the $srt variable in your browser your outputting and the mime type is set in HTML and so you see it as one line, but if you view the source it should be spanned accross multiple lines.
To ensure text output you could echo out a <pre> tag if you want to keep html or at the top of your php file add this line to force text output:
header('Content-Type: text/plain', true);
PHP_EOL The correct 'End Of Line' symbol for this platform.
So it works on local host because its window and gives a windows line break
You online website is probably on linux and gives a linux line-break
To get a consistent result use "\r\n" instead of PHP_EOL, although I think media players will be a ble to recognize any style of line breaks.
I am using the PHP code:
$numberNewline = $number . '\n';
fwrite($file, $numberNewline);
to write $number to a file.
For some reason \n appears in the file. I am on a mac. What might be the problem?
'\n' in single quotes is a literal \n.
"\n" in double quotes is interpreted as a line break.
http://php.net/manual/en/language.types.string.php
$numberNewline = $number . "\n";
fwrite($file, $numberNewline);
Try this
If inserting "\n" does not yield any results, you can also try "\r\n" which adds a "carriage-return" and "new line."
Use PHP_EOL. PHP_EOL is platform-independent and good approach.
$numberNewline = $number .PHP_EOL;
fwrite($file, $numberNewline);
PHP_EOL is cross-platform-compatible(DOS/Mac/Unix).
The reason why you are not seeing a new line is because .txt files write its data like a stack. It starts writing from the beginning, then after it finishes, the blinking line (the one indicating where your next character is going to go) goes back to the beginning. So, your "\n" has to go in the beginning.
Instead of writing:
<?php
$sampleLine = $variable . "\n";
$fwrite($file, $sampleLine);
?>
You should write:
<?php
$sampleLine = "\n" . $variable;
$fwrite($file, $sampleLine);
?>
None of the above worked for me but it was so simple - here is the code...
please use the KISS method.
echo file_put_contents("test.txt","\r\n \r\n$name \r\n$email \r\n$phone", FILE_APPEND);
It set a new blank line and then appends one line at a time.
$numberNewline = $number . '\r\n';
fwrite($file, $numberNewline);
Try This