News:

Wieners, Brats, Franks, we've got 'em all.

Main Menu

[PHP] URL Regular Expression

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

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

deadly7

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

Quote from: Blaze on September 15, 2005, 09:13:06 PM
<?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?

/golfclap  :P

Blaze

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

Sidoh

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.

Sidoh

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