This is the code.I don't know where I have made a mistake.I tried with single quote as well.
I tried it but all items come in one line.
$output_line=$item."\n";
fwrite($myfile,$output_line);
Use PHP_EOL instead of \n.
$output_line=$item . PHP_EOL ;
Use PHP_EOL which produces \r\n or \n
$data = 'some data' . PHP_EOL . 'this should be on new line';
$fp = fopen('my_file', 'a');
fwrite($fp, $data);
// File output
some data
this should be on new line
You need to use implode to add a "text" between each item.
Implode takes an array and adds a string in between each item and makes it all string.
$output_line=implode(" PHP_EOL", $item);
fwrite($myfile,$output_line);
This will now make:
Item1 PHP_EOL
Item2 PHP_EOL
Item3 PHP_EOL
Line break representation can be different on different operating system. In PHP there is an predefined constant called PHP_EOL which return the correct 'End Of Line' symbol for current platform. Available since PHP 5.0.2.
Therefore, your code should be as below:
$output_line=$item.PHP_EOL;
fwrite($myfile,$output_line);
Related
How to add \r\n next to the output: <div>%s ► %s</div>
For ex-
First output
\r\n
Second output
in:
printf("<div>%s ► %s</div>", $r['bla-bla'], $r['bla-bla']);
I have tried like:"<div>%s ► %s</div>\r\n" but it doesn't work.
Try PHP_EOL like:
<?php
printf("first line" . PHP_EOL . "SECOND line");
See also When do I use the PHP constant “PHP_EOL”?
"\n" and "\r\n", tested in text message sent by telegram bot, to create line break. Instead of showing line break, underline _ will appear after using them.
How I could printing line feed in telegram message sent by bot?
CODE
$txt = 'با تشکر از عضویت شما، هر روز حدود ساعت 10 شب یک ویدئوی جالب برای شما ارسال خواهد شد.';
$txt .= " \n ";
$txt .= 'Thanks for joining, Every day at almost 18:30 GMT an intersting video will be sent';
Message Demo
Any help will be appreciated.
There is a better way! The problem is because of URL encodings...
You can use normal PHP text using \n but by passing it to urlencode method, as follows:
$txt = urlencode("here is my text.\n and this is a new line \n another new line");
It works for me!
1) If you develop your code in Windows/Linux OS, you can simply use enter in text:
$text = 'test 123
another text';
Thats all!
2) If your code run on Windows/Linux server, you can use PHP_EOL constant instead of \n:
$text = 'text 123 '.PHP_EOL.'yet another text';
3) And if you search for an OS independent soloution, you can use %0A or chr(10) for this purpose:
$text = 'text 123 '.chr(10).'yet another text';
For future visitor just I quote #Dagon answer in comments:
Using %0A will make line feed in telegram messages
You can use %0A instead of \n.
After reading and trying all of these answers, I just wanted to post my own solution. I have an application in Laravel 5.8 that sends the reservation both by e-mail and a Telegram message.
$telegramMessage =
"<strong>Reservation Request</strong>\n".
'<strong>Name:</strong> ' . $reservation->reserv_name . "\n".
'<strong>E-mail:</strong> ' . $reservation->email . "\n".
'<strong>Phone:</strong> ' . $reservation->phone . "\n".
'<strong>Reservation Date/Time:</strong> ' . $reservation->reserv_date_time->format('d-m-Y H:i') . "\n".
'<strong>Number of people:</strong> ' . $reservation->number_of_people . "\n".
'<strong>Message:</strong> ' . $reservation->reserv_message . "\n";
Telegram::sendMessage([
'chat_id' => env('TELEGRAM_CHAT_ID', ''),
'parse_mode' => 'HTML',
'text' => $telegramMessage,
]);
More or less I have used all the html tags that Telegram API allows. You should pay attention \n must be in double quotes.
it may be not show result as you wants in Unicode languages like Persian!
you can prepare your text and use this:
$txt = implode("\n", explode('\n', $txt));
To avoid coding and encoding issues, I used this simple solution in my code:
First, I send my text message in HTML format by setting the parse_mode=HTML argument in the "sendMessage" URL.
Then, I insert the following code for each line break:
<pre>\n</pre>
ie.
... sendMessage?parse_mode=HTML&text="... paragraph1<pre>\n</pre>paragraph2 ..."
Of course the text variable was curl escaped before appended to the URL:
$text = curl_escape($handle, $text);
for me this solution works:
use double quotation mark
$message='Hi'
$message=$message."\n";
$message=$message.'Guys'
All these answers are at the same time "right" and "wrong". In fact in depend a lot of the input you have. For example if you have a text area for input and then send the content to Telegram, if the user write in the text area and press return, the text in Telegram will be
hello\neverybody
and not
hello
everybody
Performing URL encode will change nothing. After struggling a lot I discover a conflict with the fact sending the text area data from a page to another page escape some data.
The way I solve that is to remplace the escaped \n by a non-escaped one.
So:
$my_msg = str_replace("\\n","\n",$my_msg);
It works on Mac, PC and so on with text from text area.
I solved the problem in this way :
$txt = 'aaaaaaaaa\nnew line1 \nnewline2';
$parameters = array('chat_id' => $chatId, "text" => $txt);
$parameters["method"] = "sendMessage";
echo json_encode($parameters);
try here : https://telegram-bot-sdk.readme.io/docs/sendmessage
you should use urlencode to solve this problem:
$text ="sth sth
sth sth";
$text = urlencode($text);
try something like this, its work for me, and you need to add parameter parse_mode.
$text = "exampletest \n example"
or someting like this:
$text1 = 'example';
$text2 = 'next';
$data = $text1 . "\n" . $text2;
https://core.telegram.org/bots/api#formatting-options
It it easy just copy break line from anywhare to pass in code.
enter image description here
ez way😊
$txt = "Thanks for joining, Every day at </br> almost 18:30 GMT an intersting video will be sent";
or
$txt = "Thanks for joining, Every day \r\n at almost 18:30 GMT an intersting video will be sent";
I have a variable a such $var:
$var = "One, Two, Three";
I can echo the variable without any problems the output is:
One, Two, Three
Is it possible, when echoing the variable to add a line break where there is a ,, so it would look like this?
One,
Two,
Three
If you echo the text to HTML, you can do the following:
echo str_replace(",", ", <br/>", $var);
If you echo the string to a console or a text file through redirection, just use the PHP_EOL constant, which represents the correct end-of-line string for the current platform ie. "\n" for Unix, "\r\n" for Windows:
echo str_replace(",", "," . PHP_EOL, $var);
You can use this:
$var = "One,\nTwo,\nThree";
\n is the line break, and makes sense if you are working through the terminal
You use \n to force a new line when outputting to a terminal.
$var = "One,\nTwo,\nThree";
You can use the HTML <br /> to output a new line on a web browser.
$var = "One,<br />Two,<br />Three";
You can use the str_replace function once you determine which type you want.
Make use of <br> tag
$var = "One, <br>Two, <br>Three";
(or) Make use of str_replace in PHP
<?php
$var = "One, Two, Three";
echo str_replace(',',',<br>',$var); // code replaces all your commas with , and a <br> tag
Explode the string with the comma as separator, then iterate through the resulting array, adding line breaks (with the br tag if outputting to browser, or newline (\n) escape sequence if outputting to terminal) when needed?
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