5 Replies - 786 Views - Last Post: 22 July 2009 - 11:28 PM Rate Topic: -----

#1 NoPantsJim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-July 09

Parsing URLs and @ replys from Twitter strings.

Posted 22 July 2009 - 09:26 PM

I've got about 18 months of PHP under my belt, but it has been mostly casual use. I've developed a site for following tweets from a specific group of people on Twitter, and most everything is running just fine, except for one thing I can't seem to track down. I would like it if all the text from tweets reacted the same way it does on Twitter. What I mean is, if someone posts the following string of text on Twitter:

Hey @somerandomguy, have you seen the news at http://tinyurl.com/ll833p

it will show up on the other end in HTML as:

Hey <a href="http://twitter/com/somerandomguy">@somerandomguy</a>, have you seen the news at <a href="http://tinyurl.com/ll833p">http://tinyurl.com/ll833p</a>

Now, the problem is when I use the Twitter API to pull down all the feed information in XML, the <text></text> field is unformatted, just like the first above.

What I'm looking for is information on what I need on my backend in PHP to make the transition. I'm guessing it will be some form of regular expressions, which I am not very familiar with yet. I'm not asking anyone to "do my homework", I am just asking to be pointed in the right direction.

Thanks a ton,
NoPantsJim :blink:

EDIT: Interestingly enough, I notice that the forum did exactly what I am looking for to my first string, it took the plaintext URL I typed and added the proper <a></a> information to turn it into a clickable link.

This post has been edited by NoPantsJim: 22 July 2009 - 09:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Parsing URLs and @ replys from Twitter strings.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Parsing URLs and @ replys from Twitter strings.

Posted 22 July 2009 - 10:06 PM

Well you are right to be looking at regular expressions. What you need is the function preg_replace which can be used to match a pattern in the string and replace it with another string of your choice. You can use a backreference in the replacement to help create your replacement string. This is the only regular expression function I think you will need. Enjoy!

:)
Was This Post Helpful? 1
  • +
  • -

#3 NoPantsJim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-July 09

Re: Parsing URLs and @ replys from Twitter strings.

Posted 22 July 2009 - 11:10 PM

Well I found part of my solution. according to this website this code below should be able to find any URL in a string of text and replace it with appropriate <a></a> tags.

preg_replace('\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]', '<a href="\0">\0</a>', $text);

Looks like the @ replies might be a little complicated to figure out, but at least I've got some of it done.

EDIT: lmao, I presume the sad slanty face is not what the author originally intended, here it is in code tags:

preg_replace('\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]', '<a href="\0">\0</a>', $text);

This post has been edited by NoPantsJim: 22 July 2009 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5324
  • View blog
  • Posts: 27,238
  • Joined: 10-May 07

Re: Parsing URLs and @ replys from Twitter strings.

Posted 22 July 2009 - 11:13 PM

View PostNoPantsJim, on 22 Jul, 2009 - 10:26 PM, said:

Now, the problem is when I use the Twitter API to pull down all the feed information in XML, the <text></text> field is unformatted, just like the first above.

I ran into this problem as well, in my Twitter API development. I use strip_tags to clear out the html & xml.
Was This Post Helpful? 0
  • +
  • -

#5 NoPantsJim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-July 09

Re: Parsing URLs and @ replys from Twitter strings.

Posted 22 July 2009 - 11:27 PM

View Postno2pencil, on 22 Jul, 2009 - 10:13 PM, said:

View PostNoPantsJim, on 22 Jul, 2009 - 10:26 PM, said:

Now, the problem is when I use the Twitter API to pull down all the feed information in XML, the <text></text> field is unformatted, just like the first above.

I ran into this problem as well, in my Twitter API development. I use strip_tags to clear out the html & xml.


Actually that's not the issue I am facing here. simplexml_load_file() takes care of the <text></text> tags. What I mean is that the URLs in tweets are not formatted with <a></a>. Thanks for the suggestion though.

I've run into a problem, turns out the preg_replace() I posted above is invalid in someway. I am getting the following message from my cron:

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /server/location/of/my/script/cron.php on line 148

Here's the preg_replace() again:

$text = preg_replace('\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]', '<a href="\0">\0</a>', $tweet);

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5324
  • View blog
  • Posts: 27,238
  • Joined: 10-May 07

Re: Parsing URLs and @ replys from Twitter strings.

Posted 22 July 2009 - 11:28 PM

Sorry, I misunderstood the issue :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1