#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Aalborg, Denmark
    Posts
    36
    Rep Power
    15
    Hi

    I'm currently doing a site in PHP with MySQL as DB. I have a form with a textfield and I'm searching for a method to recognize "simple" URL's (eg. www.devshed.com) and store them in my DB as <a href="simple-url">simple-url</a>.

    The user who entered the simple url can edit his/her post so i also need a way to translate it back.

    The user can't write any html tags in the text (they get stripped) but i would like substitution to take place as a nice little feature.

    I've tried to do it with ereg_replace() but without any luck.

    I hope I have made myself clear



    ------------------
    // Martin
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Aalborg, Denmark
    Posts
    36
    Rep Power
    15
    HEh.. I can devshed uses the very same feature in this bbs

    ------------------
    // Martin
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Englewood, CO
    Posts
    30
    Rep Power
    15
    I wouldn't store the a href=, etc.

    Since it's always going to be that, just store the actual www.hotlinecentral.com URL and add on the a href= whenever you print it out. You're storing lots more data than you need to and you're forced to parse out the small URL every time. If you just stored the www.hotlinezone.com URL ;-), you'd save space and trouble.

    imho,

    -TM

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Aalborg, Denmark
    Posts
    36
    Rep Power
    15
    That is a good idea!

    I still need a way to recognize the url when the user submits it.

    Do you (or others) have an idea of how to do it? I've tried to locate the url with different regular patterns, but i'm relatively new to reg. expressions so any ideas are welcome.



    ------------------
    // Martin
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Helden,Limburg,The Netherlands
    Posts
    33
    Rep Power
    15
    At the moment I do not time to give the exact code but I can tell you the principle way to check for a simple URL. You should check first if there is this form:

    string.string.string
    (e.g. www.amazon.com)

    or

    string.string.string.string
    (e.g. www.amazon.co.uk)

    Then you can check (although there are a lot of exceptions) if the first string before the first dot is "WWW" But this method would leave out URL's such as webdesign.boradoli.nl so it might not be a good idea.

    The last thing you can check is whether the last (or the last two, in the second example) string consists of the most common endings like com, net, org, edu, co.uk, de, nl, no, etc...The longer you make this list, the better.

    In general the more sure you want that a string is actually an URL the more restrictive you are on the type of URL allowed.

    One alternative method could be to let PHP check if it can find index.htm, index.html, default.htm or default.html on the given URL, with a file exists function. This might take a bit more time to process, but it is a much easier and less restrictive alternative.

    ------------------
    Ramon Litjens
    Boradoli Web Design
    (www.boradoli.nl)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Aalborg, Denmark
    Posts
    36
    Rep Power
    15
    Again a good idea. Thanks.

    Your method is what i'm currently trying to use (without the proper regex, tho).

    The protocol I imagine to use is:
    1) check for [[:alnum:]]{1,}.[[:alnum:]] where the last .[[:alnum:]] can be repeated n number of times (perl style). It should also check for "/something" repeated the same way and finaly the endings "html, phtml, php, etc.) in order to catch the full url and not restrict it in any ways.
    2) if any match store it in a var
    3) check for more occurences in the same string and store them (repeat until no more matches)
    4) do the actual replacement (eg using str_replace)
    5) parse the string to the browser.

    The thing i need is any idea to write the regular search pattern (a bugger i know)

    thanx&regards

    // Martin
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Aurora
    Posts
    1
    Rep Power
    0
    Hey, I had this same problem...so I wrote my own function for it.

    I've been meaning to fix it up but it works good enough for now.

    Here is a quick idea of how I did it. I used 2 offset vars. $offset2 to hold the current position in the string, and $offset1 to hold the first occurance of 'http://' after $offset2.

    I then add the html code around the http://whatever.com string I found and store it in $url.

    Then I do

    $string = substr(0 to $offset1) . $url . substr($offset2 to end);

    Then I update the offsets accordingly.

    This will turn all http://urls.com into <a href type links.

    Here is the code, hopefully it makes sense.

    /*===============================================================================================
    | N W _ H T T P 2 H R E F [c](01-07-00) [m](01-15-00) SML |
    | |
    |-returns $str with any http:// strings changed to <a href...> |
    ===============================================================================================*/

    function nw_Http2Href($str)
    {
    $offset1 = 0;
    $offset2 = 0;
    $str_length = strlen($str);

    while($offset2 < $str_length)
    {
    // find http:// starting at char $offset2
    $offset1 = strpos($str , "http://" , $offset2);

    if(!is_int($offset1)) break;

    $offset2 = $offset1 + 7;

    /*get url, look for space or < or new line, this isnt perfect but how many urls have spaces? not many... this could use some fixing up though.*/

    while($offset2 < $str_length && $str[$offset2] != ' ' && $str[$offset2] != '<' && $str[$offset2] != 'n')
    {
    $offset2++;
    }

    // get url
    $url = substr($str , $offset1 , $offset2 - $offset1);

    // make url
    $url = "<A HREF="" . $url . "" TARGET="_new">" . $url . "</A>";

    // re - combine string
    $str = substr($str , 0 , $offset1) . $url . substr($str , $offset2);

    // 29 is the # of chars added for the link

    $str_length += 29 + ($offset2 - $offset1);
    $offset2 += 29 + ($offset2 - $offset1);
    }

    return $str;
    }




    [This message has been edited by FooKilla (edited February 29, 2000).]

Similar Threads

  1. Replies: 146
    Last Post: July 23rd, 2009, 01:55 AM
  2. index.php page recognition
    By UltraBlue in forum Windows Help
    Replies: 5
    Last Post: October 19th, 2004, 12:47 PM
  3. iris recognition data extraction
    By kansah in forum Software Design
    Replies: 1
    Last Post: January 16th, 2004, 11:58 AM
  4. Screen text recognition routine
    By Quetioner in forum C Programming
    Replies: 4
    Last Post: January 16th, 2004, 02:42 AM
  5. pl/pgsql: Columns recognition
    By kamki in forum PostgreSQL Help
    Replies: 3
    Last Post: November 12th, 2003, 12:43 PM

IMN logo majestic logo threadwatch logo seochat tools logo