Skip to content
Advertisement

How to get the indented text from the Twitter API?

I’m using the followin cURL request to get all tweets from an account:

$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);

I’m now getting the following array:

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' => 
...

My problem is that, when rendering the full_text of the tweet, the text does not follow the original indentation, since it renders like this. 1

How to display the original indentation of the tweet?

EDIT: I have the following HTML to display the text:

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

EDIT 2: according to @Pascal comment, I added <pre> tag in my <p>.

Here’s what is displayed:

enter image description here

But that’s not what I want, since I don’t want the user to have to scroll to see the text.

Advertisement

Answer

I found out how to do this. It’s ugly, but it works:

<?=str_replace(array("rn", "r", "n"), "<br/>", $tweet['full_text']); ?></p>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement