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\.
\,\;\?\'\\\=\/\_\-\#]+)?)^";
One word: Huh?
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.
Maybe you could include some information about what you need this for.
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.
If 'http://' is present, it creates <a href=" ...
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.
Someone's got to have one... :(
I'd search for www, http and ://.
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
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. (http://www.exponentcms.org/")
Hope I helped...
How about you combine method2 and 1...
Search for it, then use that function.
Why bother? Once you know its a URL, you don't need to parse it.. Just add the <a> tag.
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. (http://www.exponentcms.org/")
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.
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-
Inefficient.
$message = preg_replace("<REGULAR EXPRESSION THAT WORKS>", '<a href="\1">\1</a>', $message);
SO PWNS that. I just need a working regular expression. I'm shocked none of you have worked with them. O_o
Well, not so much you, Joe. You're a VB person =p
<?
echo findurl("ftp://www.test.org str1 http://www.test.com str2 ftp://www.x86labs.org str3");
function findurl($data) {
$ret = "";
$words = explode(" ", $data);
foreach($words as $word) {
if (substr($word, 0, 7) == "http://") {
$ret = $ret . '<a href="' . $word . '">' . $word . "</a> ";
}elseif (substr($word, 0, 6) == "ftp://") {
$ret = $ret . '<a href="' . $word . '">' . $word . "</a> ";
}else{
$ret = $ret . $word . " ";
}
}
return $ret;
}
?>
*backs away while it still works*
http://www.javaop.com/~joe/url.php
Thanks, but I'm going to stick to finding a working regular expression. :P
A WORKING? WTF U SMOKIN NGR?
pft we all know joe hardcoded those links.
<?php $seach = array(); $replace = array(); $search[] = "^(((ht|f)tp(s?))\:\/\/)(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.([a-z])(\:[0-9]+)*((\/?))(([a-zA-Z0-9\.\,\;\?\'\\\=\/\_\-\#]+)?)^"; $replace[] = '<a href="\0" target="_blank">\0</a>'; $string = "Hello. This is a test string. http://www.google.com <br /><br />I hope this serves as proof that regular expressions > all. http://sidoh.no-ip.org/reg-ex.php FTW. <br /><br /> Visit http://www.x86labs.org/forum/index.php?topic=2790.msg27101#msg27101 for more information!"; $string = preg_replace($search, $replace, $string); echo $string;?>
http://sidoh.no-ip.org/reg-ex.php
Owned.
You should be shot? =p
Sidoh, do you always mess up your code?
Quote from: Sidoh on September 14, 2005, 04:32:26 PM
<?php [b]$seach = array();[/b] $replace = array(); [b]$search[] = [/b]"^(((ht|f)tp(s?))\:\/\/)(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.([a-z])(\:[0-9]+)*((\/?))(([a-zA-Z0-9\.\,\;\?\'\\\=\/\_\-\#]+)?)^"; $replace[] = '<a href="\0" target="_blank">\0</a>'; $string = "Hello. This is a test string. http://www.google.com <br /><br />I hope this serves as proof that regular expressions > all. http://sidoh.no-ip.org/reg-ex.php FTW. <br /><br /> Visit http://www.x86labs.org/forum/index.php?topic=2790.msg27101#msg27101 for more information!"; $string = preg_replace($search, $replace, $string); echo $string;?>
http://sidoh.no-ip.org/reg-ex.php
Owned.
Know that [ php] is a syntax highlighted tag, so don't expect the bold tag will work in the future.
Additionally, since PHP contains the ability to define variables implicitly, I didn't mess up my code. It works fine, there's just memory allocated for an array that isn't used. :P
echo(findurl(""Hello. This is a test string. http://www.google.com <br /><br />I hope this serves as proof that regular expressions > all. http://sidoh.no-ip.org/reg-ex.php FTW. <br /><br /> Visit http://www.x86labs.org/forum/index.php?topic=2790.msg27101#msg27101 for more information!");
Quote from: Joe[e2] on September 15, 2005, 08:03:05 PM
echo(findurl(""Hello. This is a test string. http://www.google.com <br /><br />I hope this serves as proof that regular expressions > all. http://sidoh.no-ip.org/reg-ex.php FTW. <br /><br /> Visit http://www.x86labs.org/forum/index.php?topic=2790.msg27101#msg27101 for more information!");
o_O
You can fix it.
<?php
echo(findurl("Hello. This is a test string. http://www.google.com <br /><br />I hope this serves as proof that regular expressions > all. http://sidoh.no-ip.org/reg-ex.php FTW. <br /><br /> Visit http://www.x86labs.org/forum/index.php?topic=2790.msg27101#msg27101 for more information!"));
?>
Like that?
Quote from: Sidoh on September 15, 2005, 06:16:32 PM
Know that [ php] is a syntax highlighted tag, so don't expect the bold tag will work in the future.
Additionally, since PHP contains the ability to define variables implicitly, I didn't mess up my code. It works fine, there's just memory allocated for an array that isn't used. :P
Which is stupid. :)
Quote from: Blaze on September 15, 2005, 09:13:06 PM
<?php
echo(findurl("Hello. This is a test string. http://www.google.com <br /><br />I hope this serves as proof that regular expressions > all. http://sidoh.no-ip.org/reg-ex.php FTW. <br /><br /> Visit http://www.x86labs.org/forum/index.php?topic=2790.msg27101#msg27101 for more information!"));
?>
Like that?
/golfclap :P
I don't get it... :-[
Quote from: deadly7 on September 15, 2005, 09:13:45 PM
Quote from: Sidoh on September 15, 2005, 06:16:32 PM
Know that [ php] is a syntax highlighted tag, so don't expect the bold tag will work in the future.
Additionally, since PHP contains the ability to define variables implicitly, I didn't mess up my code. It works fine, there's just memory allocated for an array that isn't used. :P
Which is stupid. :)
Blame PHP, not me.
Okay, this is really pretty frusturating. I should have realized it earlier. My bbcode function replaces [link ] tags, which also follow the format of this regular expression. When it replaces it with the HTML <a> tag, the actual URL (which follows the pattern of the regular expression) contained in it is replaced by the regular expression. It's really annoying, and I found a way around it, but it's not ideal. I think I'm going to have to resort to using some search/buffer/replace type method like was originally suggested. Even though this is a lot slower and less efficient, it's really the only methodology I can forsee working in every case.
Here's the regular expression I used (it works great!):
$s[] = "^[\s]+(((ht|f)tp(s?))\:\/\/)(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.([a-z])(\:[0-9]+)*((\/?))(([a-zA-Z0-9\.\,\;\?\'\\\=\/\_\-\#\%\&]+)?)^";
$r[] = '<a href="\0" target="_blank">\0</a>';
Notice the "[\s]+", which indicates some amount of whitespace (space, linebreak, tab, etc) prepending the URL, as long as it's there. This works in most cases, but what if the link is at the beginning of the text or IS the entire text? Yeah, doesn't work in those situations.
Without it, though, the following regular expression causes issues because of the nature it represents:
$s[] = "#\[(link|url)=(((ht|f)tp(s?))\:\/\/)(.*?)](.*?)\[/link\]#si";
$r[] = '<a href="\2\6" target="_blank">\7</a>';
This is a [ link ] [ /link ] tag. It also works fine in isolation, but it also gets replaced with the following after the URL regular expression runs through preg_replace:
[link=<a href="<URL>"><URL></a>]<text>[/link]
There's one possible solution I can think of still retaining the regular expression methodology, but I haven't gotten it to work. That is: If it matches this string and "DISCLUDES" this string. IE it can't begin with url= or link=, which would eliminate my problem with this.
I'm going to try to mess around with a bit of search/buffer methodologies of URL replacing. I'll post with updates.