How to convert URLs in string into Links and Edit the URL while displaying?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WorldBank
    New Member
    • May 2008
    • 3

    How to convert URLs in string into Links and Edit the URL while displaying?

    I don't have good knowledge in PHP. I need help with:

    Let's suppose a string:

    "Why is sky blue? http:www.whyist heskyblue.com/whyisitsoblue/cansomebodymake _it_red"

    Ok now for this string, I want to conver the URL mentioned in to link and at a same time I want the URL (since is quite long) be displayed as:

    "Why is sky blue? http:www.whyist heskyblue.com/whyisitsob..... ."

    How will that be possible using PHP. Please help me with short and easy PHP script to do so. Thanks...
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    I could not understand what you wish to do. Can you please elaborate it?

    Comment

    • WorldBank
      New Member
      • May 2008
      • 3

      #3
      Originally posted by hsriat
      I could not understand what you wish to do. Can you please elaborate it?
      [PHP]

      $string="The world is bright. Check this URL: http://bytes.com/forum/newreply.php?do =newreply&p=318 2222";
      [/PHP]

      Now I want to convert URL in $string into link. But replacement must be done in this way:

      [HTML]
      The world is bright. Check this URL: <a href="http://bytes.com/forum/newreply.php?do =newreply&p=318 2222">http://bytes.com/forum/newreply.php?.. ....</a>
      [/HTML]

      How can I do this using PHP?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        First you need to split the string into the text and url parts.
        You can then output an <a> tag using the url. The ... part is even easier since you just need to check if the url exceeds your predefined maximum allowable length and susbtring the url at maximum allowable length - 3 (the -3 is for the dots).

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          or use &hellip; instead of three dots.

          Comment

          • WorldBank
            New Member
            • May 2008
            • 3

            #6
            I want it to be done using something like ereg_replace. Spliting the string will not be easy when there many urls in string. So I want they be replaced easily please help.

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              See if the User Contributed Notes in this page are of any use.

              Comment

              • nashruddin
                New Member
                • Jun 2008
                • 25

                #8
                Code:
                <?php
                $text = "Why is sky blue? http://www.whyistheskyblue.com/whyisitsoblue/cansomebodymake_it_red. hello world!";
                
                $new = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $test);
                
                print $new; /* will display $text with link to http://www.whyistheskyblue.com/... */
                ?>

                Comment

                • mkander
                  New Member
                  • Jun 2008
                  • 1

                  #9
                  Did you find a solution to this? I got the very same problem right now.

                  Regards,
                  Magnus

                  Comment

                  • davidc02
                    New Member
                    • Oct 2008
                    • 1

                    #10
                    This should do the trick:

                    [PHP]
                    $string = "Hello http://www.bytes.com world";

                    preg_match('/(http:\/\/[^\s]+)/', $string, $text);
                    $hypertext = "<a href=\"". $text[0] . "\">" . $text[0] . "</a>";
                    $newString = preg_replace('/(http:\/\/[^\s]+)/', $hypertext, $string);

                    echo $newString; //will output Hello <a href="http://www.bytes.com"> http://www.bytes.com</a> world

                    [/PHP]

                    Comment

                    • sitthykun
                      New Member
                      • Aug 2010
                      • 1

                      #11
                      thank davidc02

                      Comment

                      Working...