【问题标题】:New line in twitter API abraham\TwitterOAuthtwitter API abraham\TwitterOAuth 中的新行
【发布时间】:2021-04-06 13:52:02
【问题描述】:

在我的数据库中实施必要的更改以支持表情符号后,我遇到了这个问题,我似乎无法在新的行中添加新的tweetTwitter

假设我有以下代码:

$parameters = [
    'status'    => "Foo. \n New line",
];

Twitter 将正确呈现它,因为它位于 " 中。如果我碰巧做了以下事情:

$parameters = [
    'status'    => 'Foo. \n New line',
];

Twitter 将呈现Foo. \n New Line

当我需要使用新的行终止符 \n 更新状态时,我的问题就开始了。

$tweet = $dataFromDatabase;
$parameters = [
    'status' => $tweet
]

Twitter 将呈现来自数据库的文字字符串(在本例中,我们可以假设它是Foo. \n New Line)。

我想问题出在使用' 而不是"

问题

如何让Twitter 读取$tweet 变量正确显示格式正确的内容?

我尝试了以下解决方案但没有成功:

$parameters = [
    'status'    => '"'. $tweet .'"',
];
$parameters = [
    'status'    => addslashes($tweet),
];
$parameters = [
    'status'    => "{$tweet}",
];

【问题讨论】:

  • 如果数据库中的字符串 换行 - 然后 twitter 将使用它。所以,问题出在字符串格式上。
  • 这开始发生是因为我更改了表格的排序规则:charset = utf8mb4 and collation = utf8mb4_unicode_ci。之前在数据库中确实有一个新行,但现在它存储\n 而不是格式化文本。我进行了此更改以支持表情符号
  • 所以现在你需要一个脚本,它可以在你的数据库中或在发布到 Twitter 之前将 \n 替换为 "\n"
  • @u_mulder 我存储到数据库中,因为它来自用户:$x = trim($dataPassed);。知道如何将\n 转换为"\n" 吗?不需要脚本,因为这些都是新记录
  • 我不明白 - 如果用户输入新行,则新行是文本。有什么问题?

标签: php twitter-oauth


【解决方案1】:

如果要发布带有换行符的新消息,在 PHP 中,代码为:

$parameters = array("status" => "Content of the first line" . chr(13) . chr(10) . "Content of the second line");

【讨论】:

    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2022-01-03
    • 2015-06-17
    • 2015-09-25
    相关资源
    最近更新 更多