Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group URLs into single object output in API response

So I’ll try to explain this as best as possible, but I’m having some issues with how the links are being rendered so I’ll go into more detail below.

Here is the full method:

public function link_urls($text)
{
    if (!$urls = $this->get('entities', 'urls')) {
        return $text;
    }
    foreach ($urls as $url) {
        $text = str_replace(
            $url->url,
            '<a href="' . $url->url . '" target="_blank">' . $url->display_url . '</a>',
            $text
        );
    }

    return $text;
}

What this method does is it takes the ['text'] param response from the API and then it goes through and searches for URLs and appends HTML outputs.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


Let’s say that we have $text which outputs the following tweet data:

string(316) “As we continue to celebrate #BlackHistoryMonth , our next Black Artist Spotlight 🎨 is local painter @EbonyLewisArt https:// t.co/LaRa7GDk4i”

This particular tweet has 4 images, but Twitter groups them all into one "tweet" link as shown above.


Next, I’m checking the tweets $urls object that it returns, which is $urls = $this->get('entities', 'urls') which gives me:

object(stdClass)#1958 (5) {
  ["start"]=>
  int(115)
  ["end"]=>
  int(138)
  ["url"]=>
  string(23) "https:// t.co/LaRa7GDk4i"
  ["expanded_url"]=>
  string(65) "https://twitter.com/dallasmavs/status/1491079415375495168/photo/1"
  ["display_url"]=>
  string(26) "pic.twitter.com/LaRa7GDk4i"
}
object(stdClass)#1737 (5) {
  ["start"]=>
  int(115)
  ["end"]=>
  int(138)
  ["url"]=>
  string(23) "https:// t.co/LaRa7GDk4i"
  ["expanded_url"]=>
  string(65) "https://twitter.com/dallasmavs/status/1491079415375495168/photo/1"
  ["display_url"]=>
  string(26) "pic.twitter.com/LaRa7GDk4i"
}
object(stdClass)#1735 (5) {
  ["start"]=>
  int(115)
  ["end"]=>
  int(138)
  ["url"]=>
  string(23) "https:// t.co/LaRa7GDk4i"
  ["expanded_url"]=>
  string(65) "https://twitter.com/dallasmavs/status/1491079415375495168/photo/1"
  ["display_url"]=>
  string(26) "pic.twitter.com/LaRa7GDk4i"
}
object(stdClass)#1736 (5) {
  ["start"]=>
  int(115)
  ["end"]=>
  int(138)
  ["url"]=>
  string(23) "https:// t.co/LaRa7GDk4i"
  ["expanded_url"]=>
  string(65) "https://twitter.com/dallasmavs/status/1491079415375495168/photo/1"
  ["display_url"]=>
  string(26) "pic.twitter.com/LaRa7GDk4i"
}

Here is the problem:

I’m calling a foreach loop which grabs all the URLs and converts $url->url with an <a> tag and displaying the $url->display_url as shown below:

foreach ($urls as $url) {
    $text = str_replace(
        $url->url,
        '<a href="' . $url->url . '" target="_blank">' . $url->display_url . '</a>',
        $text
    );
}

The problem is that $text just has one URL to look on and the foreach loops through 4 times which causes issues and outputs the following:

As we continue to celebrate #BlackHistoryMonth , our next Black Artist
Spotlight 🎨 is local painter @EbonyLewisArt
pic.twitter.com/LaRa7GDk4i”
target=”_blank”>pic.twitter.com/LaRa7GDk4i”
target=”_blank”>pic.twitter.com/LaRa7GDk4i”
target=”_blank”>pic.twitter.com/LaRa7GDk4i

When I have one $urls object with one link in $text it’s perfectly fine, the problem shows up when Twitter groups the links in $text and outputs multiple objects.


Here are my questions:

  1. If there are multiple objects which are exactly the same, can I delete the other objects and just output one?

  2. In the foreach, if the objects are the same, can I somehow skip all but one?

All help is appreciated on this!

>Solution :

Kindly change your foreach loop to this

foreach ($urls as $url) {
    if( strpos($text, $url->url) !== false ){
        $text = str_replace(
            $url->url,
            '<a href="' . str_replace(' ', '', $url->url) . '" target="_blank">' . $url->display_url . '</a>',
            $text
        );
        break;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading