PHP:Telegram Bot:在短信中插入换行符

问题描述 投票:0回答:14

"\n"
"\r\n"
,在电报机器人发送的短信中进行了测试,以创建换行符。使用它们后,不会显示换行符,而是会出现下划线
_

如何在机器人发送的电报消息中打印换行符?

代码

$txt = 'با تشکر از عضویت شما، هر روز حدود ساعت 10 شب یک ویدئوی جالب برای شما ارسال خواهد شد.';
$txt .= " \n ";
$txt .= 'Thanks for joining, Every day at almost 18:30 GMT an intersting video will be sent';

消息演示

任何帮助将不胜感激。

php telegram-bot linefeed
14个回答
96
投票

有更好的方法!问题是由于 URL 编码... 您可以使用

\n
使用普通的 PHP 文本,但将其传递给
urlencode
方法,如下所示:

$txt = urlencode("here is my text.\n and this is a new line \n another new line");

它对我有用!


50
投票

1) 如果您在 Windows/Linux 操作系统中开发代码,您只需在文本中使用 Enter:

$text = 'test 123
         another text';

仅此而已!

2) 如果你的代码运行在 Windows/Linux 服务器上,你可以使用

PHP_EOL
常量代替
\n
:

$text = 'text 123 '.PHP_EOL.'yet another text';

3) 如果您正在寻找独立于操作系统的解决方案,您可以使用

%0A
chr(10)
来实现此目的:

$text = 'text 123 '.chr(10).'yet another text';

35
投票

对于未来的访客,我在评论中引用@Dagon 的答案:

使用

%0A
将在电报消息中进行换行


12
投票

您可以使用

%0A
代替
\n


6
投票

阅读并尝试所有这些答案后,我只想发布我自己的解决方案。我在 Laravel 5.8 中有一个应用程序,可以通过电子邮件和 Telegram 消息发送预订。

$telegramMessage = 
        "<strong>Reservation Request</strong>\n".
         '<strong>Name:</strong> ' . $reservation->reserv_name . "\n".
         '<strong>E-mail:</strong> <a href="mailto:' . $reservation->email . '"> ' . $reservation->email . "</a>\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,
        ]);

或多或少我已经使用了 Telegram API 允许的所有 html 标签。你应该注意 必须用双引号括起来。


4
投票

为了避免编码和编码问题,我在代码中使用了这个简单的解决方案:

  • 首先,我通过在“sendMessage”URL 中设置 parse_mode=HTML 参数以 HTML 格式发送文本消息。
  • 然后,我为每个换行符插入以下代码:

    <pre>\n</pre>

即。

... sendMessage?parse_mode=HTML&text="... paragraph1<pre>\n</pre>paragraph2 ..."

当然,text变量在附加到URL之前已被curl转义:

$text = curl_escape($handle, $text);

3
投票

在波斯语等 Unicode 语言中,它可能无法按您想要的方式显示结果! 你可以准备你的文本并使用它:

$txt = implode("\n", explode('\n', $txt));

2
投票

这个路😊

$txt = "Thanks for joining, Every day at </br> almost 18:30 GMT an intersting video will be sent";

$txt = "Thanks for joining, Every day \r\n at almost 18:30 GMT an intersting video will be sent";

1
投票

对我来说这个解决方案有效: 使用双引号

$message='Hi'
$message=$message."\n";
$message=$message.'Guys'

1
投票

所有这些答案同时是“正确”和“错误”。事实上,这很大程度上取决于您的输入。例如,如果你有一个文本区域用于输入,然后将内容发送到 Telegram,如果用户在文本区域中书写并按回车键,则 Telegram 中的文本将为

       hello\neverybody 

而不是

       hello
       everybody

执行 URL 编码不会改变任何内容。经过一番努力后,我发现与将文本区域数据从一个页面发送到另一个页面转义一些数据这一事实存在冲突。

我解决这个问题的方法是替换掉逃逸的 由非逃脱者。 所以:

      $my_msg = str_replace("\\n","\n",$my_msg);

它适用于 Mac、PC 等,使用文本区域中的文本。


0
投票

我是这样解决问题的:

$txt = 'aaaaaaaaa\nnew line1 \nnewline2';
$parameters = array('chat_id' => $chatId, "text" => $txt);

$parameters["method"] = "sendMessage";
echo json_encode($parameters);

在这里尝试:https://telegram-bot-sdk.readme.io/docs/sendmessage


0
投票

你应该使用urlencode来解决这个问题:

$text ="sth sth
sth sth";
$text = urlencode($text);

0
投票

尝试这样的事情,它对我有用,你需要添加参数parse_mode。

$text = "exampletest \n example"

或者类似这样的东西:

$text1 = 'example';
$text2 = 'next';
$data = $text1 . "\n" . $text2;

https://core.telegram.org/bots/api#formatting-options


-1
投票

很简单,只需从任何地方复制断行即可传递代码。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.