www.webdeveloper.com
Results 1 to 7 of 7

Thread: How do I make text into a web link

  1. #1
    Join Date
    Apr 2006
    Posts
    77

    How do I make text into a web link

    If i have a page with a web link like 'www.blah.com' is there a way I can make it into a link automatically by setting something in the website instead of putting in a <a href... each time?

    thanks

  2. #2
    Join Date
    May 2003
    Posts
    599
    I've seen pre-built javascripts that will search for any string beginning with "http://" and convert it to a link, but that wont help anybody with JS disabled.

    On the server side, it depends how you are getting your "www..." string, for example if you have these strings within a database, you could always append the string with the query to include the "<a href..."

  3. #3
    Join Date
    Apr 2006
    Posts
    77
    Can it work for both dynamic and non-dynamic content, perhaps something I could put in the top of the page. Im sure I have seen this elsewhere

    Thanks

  4. #4
    Join Date
    Apr 2007
    Posts
    1,664
    A good way to do this is using ereg_replace (or preg_replace) to recognize a link and add the HTML to make it a link. The hard part is getting the regular expression right however there are lots of libraries out there that should help.

    Here's a function by Iasmin Balaj originall posted on PHP.net
    http://www.php.net/manual/en/function.preg-replace.php

    PHP Code:
    <?php
    function hyperlink(&$text)
        {
           
    // match protocol://address/path/file.extension?some=variable&another=asf%
           
    $text preg_replace("/\s(([a-zA-Z]+:\/\/)([a-z][a-z0-9_\..-]*[a-z]{2,6})([a-zA-Z0-9\/*-?&%]*))\s/i"" <a href=\"$1\">$3</a> "$text);
       
           
    // match [url]www.something.domain/path/file.extension?some=variable&another=asf%[/url]
           
    $text preg_replace("/\s(www\.([a-z][a-z0-9_\..-]*[a-z]{2,6})([a-zA-Z0-9\/*-?&%]*))\s/i"" <a href=\"http://$1\">$2</a> "$text);
          
           return 
    $text;
        }
    ?>
    Of course this will only work for dynamic pages I'm not sure it's possible with non dynamic content. Probably with javascript interpreting the output and doing a similar thing before writing it to the page. I've always treated this as a dynamic requirement.

  5. #5
    Join Date
    Apr 2006
    Posts
    77
    Hi
    thanks, but it didnt do anything, i put it in the page

  6. #6
    Join Date
    Apr 2007
    Posts
    1,664
    Don't just copy and paste. Go learn how to use it, read the multiple tutorials. Regular expressions are tough to understand but if you want to use them you'll need to learn them. e/preg_replace will do what you want in a dynamic page and you have an example to start you off.

    Also what does this tell us other than you have a problem.
    "thanks, but it didnt do anything, i put it in the page"

    I suppose I could just respond with the same amount of information.
    >well you've got an error somewhere.

    Not much help huh. If you want help, you'll get it if you post a reasonable request for information. If you want people to write your code for you then I hear good things about the people in Pune, India. Prices are reasonable too.

  7. #7
    Join Date
    Apr 2006
    Posts
    77
    fair enough

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles