Make link live

Discussion in 'PHP' started by johneva, Aug 23, 2010.

  1. #1
    johneva, Aug 23, 2010 IP
  2. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #2
    Did a quick google search an found this. Use it when you are outputting your news data:
    
    $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:
     
    Thorlax402, Aug 23, 2010 IP
    johneva likes this.
  3. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #3
    i think you need to have a seperate page for news. like news.php.
    in database you give every news an id through auto_increment.
    and on the place where news is showndo something like this:
    
    echo "<A href='news.php?id=$row[id]'>$row[news]</a>
    
    Code (markup):
    and on seperate page get the id,retrieve the news and display it completely.
     
    arpit13, Aug 23, 2010 IP
  4. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #4
    Cheers thats looks like its what I am after will give it a go now.

     
    johneva, Aug 23, 2010 IP
  5. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #5
    Hmm just had a quick play with it an carnt seem to get it to work.

              
      <p> <?php
             $string = $news;
             preg_match('/(http:\/\/[^\s]+)/', $string, $text);
             $hypertext = "<a href=\"". $text[0] . "\">" . $text[0] . "</a>";
             $newString = preg_replace('/(http:\/\/[^\s]+)/', $hypertext, $string);
      
              echo nl2br("$newString"); ?>
      </p>
    
    
    PHP:
    EDIT

    Ignore me I know why its cos its looking for http:// and the text only starts www.

    I will just edit the text in the database to have the http://

    Works great cheers.
     
    Last edited: Aug 23, 2010
    johneva, Aug 23, 2010 IP
  6. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #6
    Glad to hear it works since I didn't get the chance to test it myself. Probably gunna have to use this in the future.
     
    Thorlax402, Aug 23, 2010 IP