Help - Search - Member List - Calendar
Full Version: Extracting URLs from a string using a Regular Expression
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
erin g
I am using a convoluted way of extracting URLs from a long string. I've
been using the explode() function multiple times and creating temp
arrays until i explode them down enough to get just the URLs.

I'd rather do it using a regular expression but I am having trouble
finding a good example of this.

Can anyone post a good regular expression to extract the URLS from a
string and save them in an array?

Thanks a lot!
Erin G

Stefan Rybacki
erin g wrote:
QUOTE
I am using a convoluted way of extracting URLs from a long string. I've
been using the explode() function multiple times and creating temp
arrays until i explode them down enough to get just the URLs.

I'd rather do it using a regular expression but I am having trouble
finding a good example of this.

Can anyone post a good regular expression to extract the URLS from a
string and save them in an array?

Thanks a lot!
Erin G

hum, something like this?:


/(http://[W]+)/

Veikko Mkinen
erin g wrote:
QUOTE
I am using a convoluted way of extracting URLs from a long string. I've
been using the explode() function multiple times and creating temp
arrays until i explode them down enough to get just the URLs.

I'd rather do it using a regular expression but I am having trouble
finding a good example of this.

Can anyone post a good regular expression to extract the URLS from a
string and save them in an array?

To save matches into an array see http://www.php.net/preg_match_all
(note the $matches parameter). To make a universal url catching regexp
isn't that simple... maybe something along the lines of "http://w+"
but this will fail if you have a sentence that ends with a url (eg.
"Come and read my blog from http://myblog.invalid.")


-veikko

--
veikko
mail@ .com
makinen


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.