Author Topic: [PHP] URL Regular Expression  (Read 16410 times)

0 Members and 1 Guest are viewing this topic.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
[PHP] URL Regular Expression
« on: September 11, 2005, 10:52:44 pm »
Anyone have one that works?  I've found a few on the internet, but it's been so long since I've created my own regular expressions.

I'm looking for one that follows the syntax:

<protocol>://<subdomain>.<domain>.<com/net,etc>

Thanks in advance!

Edit --

Since no one who posted in this thread was able to come up with one, I was forced to do it on my own!  *sob*

So none of the rest of you have to suffer through such horrid events, I'll post my solution at the top of the thread:

Code: [Select]
$search[] =

"^(((ht|f)tp(s?))\:\/\/)(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.([a-z])(\:[0-9]+)*((\/?))(([a-zA-Z0-9\.

\,\;\?\'\\\=\/\_\-\#]+)?)^";
« Last Edit: September 14, 2005, 04:37:55 pm by Sidoh »

Offline deadly7

  • 42
  • Moderator
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: [PHP] URL Regular Expression
« Reply #1 on: September 11, 2005, 10:59:13 pm »
One word: Huh?
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
 [17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] URL Regular Expression
« Reply #2 on: September 11, 2005, 11:03:41 pm »
This is the one I found:

Code: [Select]
/^(((ht|f)tp(s?))\:\/\/)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$/
I'm not too sure what I need to begin or truncate that string with, but it doesn't work with preg_replace.

Offline Quik

  • Webmaster Guy
  • x86
  • Hero Member
  • *****
  • Posts: 3262
  • \x51 \x75 \x69 \x6B \x5B \x78 \x38 \x36 \x5D
    • View Profile
Re: [PHP] URL Regular Expression
« Reply #3 on: September 11, 2005, 11:04:45 pm »
Maybe you could include some information about what you need this for.
Quote
[20:21:13] xar: i was just thinking about the time iago came over here and we made this huge bomb and light up the sky for 6 min
[20:21:15] xar: that was funny

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] URL Regular Expression
« Reply #4 on: September 11, 2005, 11:31:50 pm »
Like this:

http://www.google.com

See how SMF automatically generates an ancor tag becaue it recognizes it as a link?  That's what I'm wanting.

Offline Quik

  • Webmaster Guy
  • x86
  • Hero Member
  • *****
  • Posts: 3262
  • \x51 \x75 \x69 \x6B \x5B \x78 \x38 \x36 \x5D
    • View Profile
Re: [PHP] URL Regular Expression
« Reply #5 on: September 11, 2005, 11:39:15 pm »
If 'http://' is present, it creates <a href=" ...
Quote
[20:21:13] xar: i was just thinking about the time iago came over here and we made this huge bomb and light up the sky for 6 min
[20:21:15] xar: that was funny

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] URL Regular Expression
« Reply #6 on: September 11, 2005, 11:50:22 pm »
It's more elaborate than that.

ftp://www.something.com

And I would probably settle for that, but I'm not sure how to translate it into a regular expression, which is why I'm creating this post.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] URL Regular Expression
« Reply #7 on: September 12, 2005, 05:29:59 pm »
Someone's got to have one... :(

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: [PHP] URL Regular Expression
« Reply #8 on: September 12, 2005, 06:57:00 pm »
I'd search for www, http and ://.
And like a fool I believed myself, and thought I was somebody else...

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] URL Regular Expression
« Reply #9 on: September 12, 2005, 07:02:48 pm »
I'd search for www, http and ://.

I know that... :P

I want it translated into a Regular Expression.  That's why I put regex in the title!  :D

Offline Ryan Marcus

  • Cross Platform.
  • Full Member
  • ***
  • Posts: 170
  • I'm Bono.
    • View Profile
    • My Blog
Re: [PHP] URL Regular Expression
« Reply #10 on: September 13, 2005, 04:39:19 pm »
Its not all that hard.

Method 1: Split the message into an array seperated by spaces. One element per word. Then, use parse_url. Slow and clunky.
Method 2: Use strpos to search find "http", "://", ".com", ".net" and ".org". Use a buffer type method.
Method 3: Use a good WYSWYG web based editor, like the one in exponent.

Hope I helped...

Thanks, Ryan Marcus

Quote
<OG-Trust> I BET YOU GOT A CAR!
<OG-Trust> A JAPANESE CAR!
Quote
deadly: Big blue fatass to the rescue!
496620796F75722072656164696E6720746869732C20796F75722061206E6572642E00

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: [PHP] URL Regular Expression
« Reply #11 on: September 13, 2005, 04:40:33 pm »
How about you combine method2 and 1...

Search for it, then use that function.
And like a fool I believed myself, and thought I was somebody else...

Offline Ryan Marcus

  • Cross Platform.
  • Full Member
  • ***
  • Posts: 170
  • I'm Bono.
    • View Profile
    • My Blog
Re: [PHP] URL Regular Expression
« Reply #12 on: September 13, 2005, 04:41:43 pm »
Why bother? Once you know its a URL, you don't need to parse it.. Just add the <a> tag.
Thanks, Ryan Marcus

Quote
<OG-Trust> I BET YOU GOT A CAR!
<OG-Trust> A JAPANESE CAR!
Quote
deadly: Big blue fatass to the rescue!
496620796F75722072656164696E6720746869732C20796F75722061206E6572642E00

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] URL Regular Expression
« Reply #13 on: September 13, 2005, 04:49:51 pm »
Its not all that hard.

Method 1: Split the message into an array seperated by spaces. One element per word. Then, use parse_url. Slow and clunky.
Method 2: Use strpos to search find "http", "://", ".com", ".net" and ".org". Use a buffer type method.
Method 3: Use a good WYSWYG web based editor, like the one in exponent.

Hope I helped...


You're thinking of too abstract a method.

I want a regular expression that defines a URL.  I posted one, but there's obviously something incorrect about it--it doesn't work.

Regular Expressions are vastly more efficient than anything you posted.  There's no reason for me to use a WYSIWYG editor.  I'm developing a function that will search and define links in dynamic content.  I just need a regular expression that accurately defines a URL.  After this, I'd just use preg_replace to replace all URL's to a URL+Anchor.

It's been a long time since I've worked with regular expressions much, and I was hoping there was someone here who's more fresh with them than I.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [PHP] URL Regular Expression
« Reply #14 on: September 13, 2005, 10:53:25 pm »
for(int i = 0; i < strlen(data); i++) {
  if (substr(data, i, 7) == "http://") {
    // url starts here
  } elseif (substr(data, i, 6) == "ftp://") {
    // omfg another url!
  } elseif (something) {
    // you get the drift
  } else {
    // LOL NOTHING
  }
}


EDIT -
I had my less than sign backwards, as usual. -sigh-
« Last Edit: September 13, 2005, 11:01:14 pm by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.