News:

Pretty crazy that we're closer to 2030, than we are 2005. Where did the time go!

Main Menu

[PHP] URL Regular Expression

Started by Sidoh, September 11, 2005, 10:52:44 PM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

Sidoh

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:

$search[] =

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

\,\;\?\'\\\=\/\_\-\#]+)?)^";

deadly7

[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

Sidoh

This is the one I found:

/^(((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.

Quik

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

Sidoh

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.

Quik

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

Sidoh

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.

Sidoh


Blaze

And like a fool I believed myself, and thought I was somebody else...

Sidoh

Quote from: Blaze on September 12, 2005, 06:57:00 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

Ryan Marcus

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

Blaze

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...

Ryan Marcus

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

Sidoh

Quote from: Ryan Marcus 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...


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.

Joe

#14
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-
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.