views:

498

answers:

3

Hey all,

I have a string ($string for this example) and I want to do the following: Parse the string and convert all links within it to "actual links" (adding the open/close anchor tag) -- what is the best way to do this? I tried using preg_split() to create an array of the links contained in the string, with the idea of using str_replace() after the fact to make the changes. This approach isn't working however because I am having a hard time isolating the links in the string of text w/ preg_split().

$string could contain several links within it so thats why I was going with this approach. If I knew precisely what the links were ahead of time or if str_replace worked with pattern matching I would be set.

In short, I don't the approach described above as being the best way to go. What is a better method?

  • Nicholas
+1  A: 

I think you're misunderstanding the purpose of preg_split(). It's used to split a string apart based on a regex. For example if you have a data string "one1two2three3four" you could use preg_split with a regex of "\d" and you'd get a 4-element array returned, consisting of "one", "two", "three", "four".

So unless the links are always separated by specific characters, preg_split() isn't the right tool for the job. You'll probably want to use preg_match() and then loop over the different matches that are returned.

Edit: or as David stated, preg_replace() is even better than preg_match().

Chad Birch
+2  A: 

This is precisely the kind of thing preg_replace was intended for...

preg_replace('/your URL regex/', '<a href="$0">$0</a>', $string);

EDIT: whaddya know, top Google hit for "URL regex PHP": http://snipplr.com/view/2371/regex-regular-expression-to-match-a-url/

# PHP Example: Automatically link URL's inside text.

$text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);

Note that if you want to match URLs in full generality, you'll need a devilishly complicated regular expression, but to match most common HTTP/HTTPS URLs, the above regex should work.

David Zaslavsky
Absolutely perfect David, I was making this much more complicated than it needed to be. Thank you.
Nicholas Kreidberg
Trying to come up with a regex that accomodates urls like: http://www.lasvegassun.com/blogs/ralstons-flash
/2009/feb/17/caucus-within-gop-assembly-caucus
----get-out-your-/ -- it breaks at the "-flash"
Nicholas Kreidberg
Figuring out the right regex to match URLs probably deserves a question of its own (and it may have been asked already)
David Zaslavsky
A: 

The regular expression depends on how the links will appear, or where they can appear (within quotes, inside a custom HTML Tag, etc).

It also depends on where the input is coming from. If it is coming from a user, it might be a better idea to use a markdown-like syntax.

At any rate, check out: http://regexlib.com/Search.aspx?k=links for some regular expressions that claim to match URIs and such. You would then use (originally posted by David):

preg_replace('/your URL regex/', '<a href="$0">$0</a>', $string);
Nick Presta