[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

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

Joe

#16
<?  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
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.


Sidoh

Thanks, but I'm going to stick to finding a working regular expression.  :P

Joe

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.


Mythix

pft we all know joe hardcoded those links.
Philosophy, n. A route of many roads leading from nowhere to nothing.

- Ambrose Bierce


Sidoh

#20
<?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.

Joe

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.


Sidoh


deadly7

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

#24
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

Joe

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


Sidoh

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

Joe

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.


Sidoh


Blaze

<?phpecho(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?
And like a fool I believed myself, and thought I was somebody else...