Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: eregi_replace; recognize hyperlinks automatically

  1. #1
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile eregi_replace; recognize hyperlinks automatically

    Hi there

    I would like to convert webadresses in text from my database automatically into hml hyperlinks. The pattern for recognizing it is that it starts with "http" (or "www") and ends with any word-separator. I have googled the issue without finding anything that works for me. I tried this out:
    PHP Code:
    $text eregi_replace(
          
    '\\[http]([-_./a-z0-9!&%#?+,\'=:;@~]+)\\[\b]',
          
    '<a href="\\1">\\1</a>'$text); 
    - as [\b] is supposed to be the regular expression for any word-separator. I tried a couple of other options too without success.

    Can anyone see what's wrong?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $text = preg_replace('/https?:\\/\\/([-_.\\/\w\d!&&#37;#?+\\,\\\\'=:;@~]+)/i', '<a href="$1">$1</a>', $text);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks!
    I haven't tried it out yet but will very soon.
    Jonas

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Two tiny problems with it. I love it Twey, but I noticed it was missing a \.

    Code:
    $text = preg_replace('/https?:\\/\\/([-_.\\/\w\d!&&#37;#?+\\,\\\\\'=:;@~]+)/i', '<a href="$1">$1</a>', $text);
    I really am only an elementary student of this stuff. There needs to be an alternate statement to allow for missing 'http://' in case a person left that out.

    Even when using the code listed with the '/' put in I get:

    Code:
    $text="http://www.what.com"
    http://www.mysite.com/www.what.com
    Where mysite is the webmaster's address.

    EDIT: Part of the problem seems to be on my end. I am not sure why my links are all being prefaced with my website's url.

    EDIT: nvm. problem fixed, but still having trouble with the regular expression.

    EDIT: kk, got it. Try the following:

    PHP Code:
    $text preg_replace('/https?:\\/\\/([-_.\\/\w\d!&%#?+\\,\\\\\'=:;@~]+)/''<a href=http://$1>$1</a>'$text); 
    I changed <a href="$1"> to <a href=http://$1> Gonna add this one to my collection. It is a pretty cool idea, thanks

    I almost have the one for the www.site.com addresses where there is no "http://" typed in, but I can't quite figure out how to remove the . in front of the hyperlinked text. Otherwise it works except for the cosmetic problem.
    PHP Code:
    $text preg_replace('/wwws?([-_.\\/\w\d!&%#?+\\,\\\\\'=:;@~]+)/''<a href=http://www$1>$1</a>'$text); 
    Anyone know how to work out the bugs? For example the two don't seem to work together.
    Last edited by james438; 08-21-2007 at 05:11 AM.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    preg_replace('/(https?:\\/\\/[-_.\\/\w\d!&&#37;#?+\\,\\\\\'=:;@~]+)/i', '<a href="$1">$1</a>', $text);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    It's a bit sloppy, but here is what I am using to hyperlink urls that are of both formats.
    PHP Code:
    $text=preg_replace('/(https?:\\/\\/[-_.\\/\w\d!&%#?+\\,\\\\\'=:;@~]+)/i''<a href="$1">$1</a>'$text);
    $text=preg_replace('/[^\/](wwws?[-_.\\/\w\d!&%#?+\\,\\\\\'=:;@~]+)/i''<a href="$1"> $1</a>'$text); 
    The only problem is that this assumes the typed in "www" url (as opposed to the "http" url) is preceded by a space.
    Last edited by james438; 08-22-2007 at 06:43 AM.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Try:
    Code:
    /^((?:https?:\/\/|www\d?\.)(?:www\.)?[^\b]+)/
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I am just not quite at that level yet. How would the code look if you were to integrate it into the script you want me to integrate it into. Are we talking about one preg_replace() or two? For example I do not understand the ?:term?: command just yet or why you are looking for a digit right after "www" as in the case "?:www\d?\."
    Last edited by james438; 08-23-2007 at 12:30 AM.

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Exactly the same as it does now, just with the regex replaced:
    Code:
    $text=preg_replace('/^((?:https?:\/\/|www\d?\.)(?:www\.)?[\w\d.&#37;+-]+)/i', '<a href="$1">$1</a>', $text);
    Are we talking about one preg_replace() or two?
    One.
    For example I do not understand the ?:term?: command
    It's just a group, like () but without capturing.
    just yet or why you are looking for a digit right after "www" as in the case "?:www\d?\."
    A lot of sites number servers that they use for load-balancing, as in www3.mysite.com. I thought it's better to be safe than sorry.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Sorry, just not getting the code you posted to work. I recently learned a lot about captures in some more depth, but it was a hard concept to understand for me at first. Next up are some of the other commands that I don't often use like ?:, \b, ?=, or (?!pattern) and the like. I do have a working version anyway that I posted above that will convert both formats into hyperlinked urls. I just use two preg_replaces and a str_replace or two for cosmetic reasons.
    Last edited by james438; 08-23-2007 at 11:29 PM.

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
  •