Results 1 to 6 of 6

Thread: Auto Parsing Links

  1. #1
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default Auto Parsing Links

    How would I go about parsing links from an input when the link is contained within some other text.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    You mean:
    Code:
    <form method="post" action="?send">
    <b>URL</b>:
    <input type="text" name="url">
    <br><input type="submit" value="Enter">
    <hr>
    <?php
    $s = $_GET["send"];
    if (isset($s)) {
      echo "<a href=\"".$_POST["url"]."\">My link</a>";
      }
    ?>
    </form>
    - Mike

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    yes and no I understand how to parse the link once its recognized, im asking about the input being more then just a link... eg a comments box, but if they enter in http:// or www. then its going to be a link...

    PHP Code:
    if(preg_match(/[(http://) | (www.)/) {
          
    echo "<a href="MATCH REST OF LINK">FULL URL</a>";


    That would print out http:// or www. if thats printed, but what about the rest of the link ... and also ... i want it to search the full text entry, not just one line

    difference between entering a url and a comment?

    but i want to parse all urls that are given inside the comment part

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Oh I see. I use this on my wiki site:
    Code:
    function url_links($string){
    //"www."
       $pattern_preg1 = '#(^|\s)(www|WWW)\.([^\s<>\.]+)\.([^\s\n<>]+)#sm';
       $replace_preg1 = '\\1<a href="http://\\2.\\3.\\4" target="_blank">\\2.\\3.\\4</a>';
    
    //"http://"
       $pattern_preg2 = '#(^|[^\"=\]]{1})(http|HTTP|ftp)(s|S)?://([^\s<>\.]+)\.([^\s<>]+)#sm';
       $replace_preg2 = '\\1<a href="\\2\\3://\\4.\\5" target="_blank">\\2\\3://\\4.\\5</a>';
      
       $string = preg_replace($pattern_preg1, $replace_preg1, $string);
       $string = preg_replace($pattern_preg2, $replace_preg2, $string);
    
       return $string;
    }
    - Mike

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    (http|HTTP|ftp)(s|S)?://
    I understand that, but can you explain the rest of the reg expression?

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I hardly understand myself, I got it from PHP.net... hehe.

    I might be able to break it down though:
    Code:
    #(^|\s)(www|WWW)\.([^\s<>\.]+)\.([^\s\n<>]+)#sm
    Any number of characters between \. and \. (which are just dots "."). The last segment, caries with the .extension.

    The other regex's are just implentations of that one expanded a bit.
    - Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •