Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post

    Parse a url and convert to link.

    Say I have my string
    Code:
    $mess = "visit www.codingforums.com";
    , how can I convert that into a link?

    Should I split up to variable into an array and search it and then go from there? (Can PHP do that?)
    Last edited by Mhtml; 04-06-2003 at 06:07 AM.
    Omnis mico antequam dominus Spookster!

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Is this for your shout box? Trying to automatically parse URL's and create hyperlinks out of them?

    Yes you can parse the strings looking for patterns that might be a URL. RegEx would be your best bet. Not good enough with them to come up with a pattern for that though. Of course you could always download any message board software like this one and look at how they do it.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yup it's for my shout box.
    PHP-Dev came with invision board so I'll have a look in the source.

    I'd never had thought of that myself. Thanks spookster!
    Omnis mico antequam dominus Spookster!

  • #4
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Well after a lot of searching through both phpBB and Invision Board I've given up. lol Just couldn't find it.... I googled as well.. no such luck.
    Omnis mico antequam dominus Spookster!

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    I too am regex challenged .... luckily I reckon Mordred should turn up soon

    but , and the reason I posted is .... use the manual luke..

    if you look at preg_replace in the manual (download the CHM version) there are loads of examples , I grabbed this from there , its not perfect but anyway...

    (the bold is to stop the forum stuffing up the slashes)

    Code:
    <?
    $text="
    `oh sweet yakdom`  by da yak http://www.yaks.com , 
    get the ripped CD @ ftp://yaks.net/cd?llamas=smelly , cool
    ";
    
    $text = preg_replace(
    "/(?:^|\b)((((http|https|ftp):\/\/)|(www\.))([\w\.]+)([,:%#&\/?=\w+\.-]+))(?:\b|$)/is",
    "<a href=\"$1\" target=\"_blank\">$1</a>",
    $text
    );
    
    echo $text;
    ?>
    I have a version of the phpBB BBcode parser but its pretty `slack` at best (search for bbcode.php) & I cant find anything in the invision code which was designed my Martians (well designed I mean , but by aliens)
    Last edited by firepages; 04-06-2003 at 12:58 PM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    I have the CHM version of the manual. I was looking all through it for stuff on regex, must have missed that example.
    Omnis mico antequam dominus Spookster!

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try this
    PHP Code:
    <?

    $mess 
    "visit www.codingforums.com";
    $url $mess;

    echo (
    "<a href=\"http://" preg_replace("/.+ w{3}/","www",$url). "\">$mess</a>");
    ?>
    Which will match

    'any number of characters' followed by " www" followed by 'anything'

    and turn it into

    <a href="http://wwwanything">any number of characters</a>
    Last edited by brothercake; 04-06-2003 at 01:44 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #8
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Well that works brothercake but it will convert the whole string into a link no matter what.

    The previously posted one works fine except for when the user doesn't enter http:// so I'll have to figure that bit out.
    Omnis mico antequam dominus Spookster!

  • #9
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Buh, no such luck. I tried for a while, I got close to making it add http:// onto www.domain.com addresses but I somehow kept repearing the address a few times and adding /http:// lol.
    Omnis mico antequam dominus Spookster!

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I assumed you want the string to match "anything folowed by www.domain.com"; if you can give more precise details of what you want it to match I could probably write the regexp for you
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #11
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    I want it to match a url, or at least a string resembling it and convert that url into a hyperlink.

    The current one I have is the one before you posted, it works fine with the exception that it doesn't work it you don't have http:// infront of your link..

    IE www.domain.com becomes <a href="www.domain.com">www.domain.com</a> but needs to be href="http://www.domain.com">www.domain.com</a>

    Am I explaining enough?
    Omnis mico antequam dominus Spookster!

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry man - I can't work it out I suggest posting this again, as a regex problem
    Last edited by brothercake; 04-17-2003 at 03:03 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #13
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    WOOT! I have figured out my own shorter (for the time being) version of converting links.. .YAY!

    Have been playing with regular expressions all day, all of a sudden a light bulb whent off over my head and I am no longer regex challenged.

    I have to run a few more tests before I post the code but my initial and only test worked.
    Omnis mico antequam dominus Spookster!

  • #14
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hehe, welcome to the wonderful world of RegExps...
    URLs are a hard and moving target for RegExps, because the definition what a URL (or URI) constitutes is rather broad, so it's usually quite a daunting task to create a RegExp that suits most possibilities... I often use simpler ones that leave the freedom to enter the occasional non-sensical urls.

    Here's the URI matching RegExp of the offical german PHP newsgroup FAQ, provides perhaps some inspiration:

    Code:
    /**
    * replace URIs with appropriate HTML code to be clickable.
    */
    function replace_uri($str) {
      $pattern = '#(^|[^\"=]{1})(http://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>]|$)#sm';
      return preg_replace($pattern,"\\1<a href=\"\\2\\3\"><u>\\2\\3</u></a>\\4",$str);
    }

    BTW: And just one test means more or less nothing. Especially with RegExps.

  • #15
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Lol ... I found that out pretty quickly.
    I've gone through a quite a few different variations and currently I'm using:
    PHP Code:
    preg_replace("/([^A-z0-9])(http|ftp|https)([\:\/\/])(.*)([^A-z0-9])/","<a href=\"$2$3$4\">$2$3$4</a>",$message); 
    I did have a better one but I'll have to figure out what it was because I can't remember.. .The problem is that it doesn't stop when it gets to a space in that regexp.

    I think I'll start again.
    Omnis mico antequam dominus Spookster!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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