首页 > 解决方案 > 如何从 Twitter API 获取缩进文本?

问题描述

我正在使用以下 cURL 请求从一个帐户获取所有推文:

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$requestMethod = 'GET';
$getfield = '?screen_name=heregoestheaccount&tweet_mode=extended&count=50';
$twitter = new TwitterAPIExchange($settings);
$twitter->setGetfield($getfield);
$twitter->buildOauth($url, $requestMethod);
$response = $twitter->performRequest(true, array(CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0));
$tweets = json_decode($response, true);

我现在得到以下数组:

array (size=26)
  'created_at' => string 'Fri Sep 25 12:05:51 +0000 2020' (length=30)
  'id' => int 1309464126494846976
  'id_str' => string '1309464126494846976' (length=19)
  'full_text' => string '[ #Esports ]

On Sunday 27.09.2020 at 1 p.m. the First Division Qualifier for the Rainbow Six: Siege Fall Season 2020 will start! Our team will fight for its slot in the first division.

We wish all the best for their first competition in the noetic team!

#noeticBLAST  (length=302)
  'truncated' => boolean false
  'display_text_range' => 
    array (size=2)
      0 => int 0
      1 => int 272
  'entities' => 
...

我的问题是,在渲染full_text推文时,文本不遵循原始缩进,因为它是这样渲染的。 1

如何显示推文的原始缩进?

编辑:我有以下 HTML 来显示文本:

<div class="col-sm-12 col-md-7">
    <p>
        <?= $tweet['full_text']; ?>
    </p>
</div>

编辑 2:根据@Pascal 评论,我<pre>在我的<p>.

这是显示的内容:

在此处输入图像描述

但这不是我想要的,因为我不希望用户必须滚动才能看到文本。

标签: phptwitter

解决方案


我发现了如何做到这一点。这很丑陋,但它有效:

<?=str_replace(array("\r\n", "\r", "\n"), "<br/>", $tweet['full_text']); ?></p>

推荐阅读