views:

940

answers:

6

Let's say I have a big RSS feed full of Twitter posts, and they are all plain text. Lots of the posts contain URLs, and I'd like those URLs to be turned into links.

So I've got a variable that is equal to:

Visualization of layoffs by industry, number and date. Looking forward to seeing similar for hiring trends. http://bit.ly/XBW4z

And I'd like it to turn into:

Visualization of layoffs by industry, number and date. Looking forward to seeing similar for hiring trends. http://bit.ly/XBW4z

How could I do that? I am useless when it comes to regex and its ilk, so help is much appreciated!

+2  A: 
configurator
What about digits or other valid characters?
Gumbo
OK, so that's the regex. How do I actually apply it to my chunk-of-text variable? Am I using regex, or preg_replace, or what? (I did not say I was useless with regex for nothing...)
Eileen
I don't know php yet, sorry.
configurator
Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems...
configurator
HA! My thought is usually, "Oh crap, maybe this needs a regular expression"
Eileen
I don't think that this expression will handle query parameters properly.
Boden
@Boden: Why not?
configurator
+2  A: 

Even I want one... Check the first link in the search result.Its pretty old!

and BTW look at the RHS.. we have similar questions. http://stackoverflow.com/questions/341942/recognize-url-in-plain-text http://stackoverflow.com/questions/635247/regex-for-url-and-image-within-a-text-or-html

Shoban
Wow, actually that's the first time I see someone who is not getting bashed for posting a Google link.
Tomalak
@Tomalak .. I have seen people shouting for not using google search! Show me the question which you are talking about. I want to see ;-)
Shoban
A: 

There are regular expressions that match valid URLs. For example the the complete regular expression for URLs, that’s derived from the grammar definition of URLs.

But it’s better to explicitly declare those than trying to find them. Because there are some situations in which it cannot be distinguished, if some characters are part of the URL or just text.

Gumbo
+1  A: 

OK, this question here (http://stackoverflow.com/questions/635247/regex-for-url-and-image-within-a-text-or-html) has a baffling title, but a helpful answer at the bottom. At least, it works for me and my cases!

$text = preg_replace('@(http://([\w-.]+)+(:\d+)?(/([\w/_.]*(\?\S+)?)?)?)@', 
                 '<a href="$1">$1</a>', $text);
Eileen
But just in your case. URLs like `http://example.com/foo-bar` or `http://example.com/foo#bar` are not matched.
Gumbo
@Eileen: Hm... The "helpful answer at the bottom" has been voted -1. This is at least a hint that it could be flawed.
Tomalak
True, but as I said it works for all of my cases, AND except for Boden it the only answer in all of the proposed answers that actually shows how to perform the replacement in PHP. Giving me complicated (but perfect!) regex is useless without the PHP to make it work.
Eileen
+1  A: 

Look at the preg_replace function. So something like this:

$regex_url = "((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)";

preg_replace($regex_url, '<a href="$1">$1</a>', $your_input_string);

Regular expression for URL taken from: http://www.geekzilla.co.uk/view2D3B0109-C1B2-4B4E-BFFD-E8088CBC85FD.htm

Boden
A: 

just to add some info ... check this class on phpclass.org, will solve ur problem ... this class will find the links and will convert them as well ...

http://www.phpclasses.org/browse/package/6114.html