Hi Is there an easy way to make any links that come from a database clickable, its for a news section I am working on and sometimes in with the rest of the text there is a www.site.com for example just wondering if there is an easy way to make these live. Example: http://www.lease-hire.co.uk/dual_control_car_hire/news_article/Newest-News/11 See near the bottom of this text www.driving.org/youngdriver
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:
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.
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.
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.