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

0 Members and 2 Guests are viewing this topic.

Offline deadly7

  • 42
  • Moderator
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: [PHP] URL Regular Expression
« Reply #30 on: September 15, 2005, 09:13:45 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

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] URL Regular Expression
« Reply #31 on: September 15, 2005, 09:16:01 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

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: [PHP] URL Regular Expression
« Reply #32 on: September 15, 2005, 11:20:58 pm »
I don't get it...  :-[
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 #33 on: September 15, 2005, 11:38:23 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.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] URL Regular Expression
« Reply #34 on: October 03, 2005, 09:33:01 pm »
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!):

Code: [Select]
$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:

Code: [Select]
$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.
« Last Edit: October 03, 2005, 09:35:12 pm by Sidoh »