Results 1 to 10 of 10

Thread: regular expression of links.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    74

    regular expression of links.

    hello,
    I am building a system that allows users to enter text.
    something like a guestbook.

    I would like to do that if the user write a link that starts with www in the text it will turn the link into a clickable link that means it will add the <a href=""></a> tag to it.

    is there any regular expression that can help me with such thing?
    thanks in advanced for any help.

    Yair
    -------------------------------------
    http://www.ybweb.com

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    This expression should do the same as the link finder of vBulletin.

    "(www\.[^\w]+)|(http://[^\w]+)"

    You need to add the http:// if it isn't already there, else the link is interpreted as relative.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    74

    thank you

    I will now try to find a place which will help me to understand this regular expression.

    thanks again

    Yair
    -------------------------------------
    http://www.ybweb.com

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Err... ask me?

    The expression matches one of two patterns. The first pattern is
    (www\.[^\w]+)
    Which means "www." followed by 1 or more non-whitespace characters. Captured.

    The second pattern is
    (http://[^\w]+)
    Which mean "http://" followed by 1 or more non-whitespace characters. Captured.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    74

    thanks

    I didnt wanted to bother you with lazyness questions ...



    thank you
    Yair
    -------------------------------------
    http://www.ybweb.com

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Everyone else does...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    CornedBee, I got an error (Unknown modifier '|') when I tried that.

    I've always used:

    Code:
    $text = preg_replace('/(?<!<a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i', '<a href="\\0">\\0</a>', $text);
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    'tis weird, | is one of the most basic features of regular expressions.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by CornedBee
    'tis weird, | is one of the most basic features of regular expressions.
    Does the expression work for you?

    I tried changing it to:

    Code:
    "((www\.[^\w]+)|(http://[^\w]+))"
    Thinking that might help. It gets rid of the error, but doesn't do anything to the text.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Haven't tried. I just think it's weird that I get such a simple expression wrong
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width