npsari Posted June 2, 2007 Share Posted June 2, 2007 dear programmers Users come to my website and submit to databse TEXT stuff Sometimes, they mention their website adrress http://www.example.com But the website doesnt show as an clickable link (like here in the forums do) It shows as normal text How can i tell the page to display links when www.com format is used Quote Link to comment https://forums.phpfreaks.com/topic/53921-solved-create-active-links-automatically-using-php/ Share on other sites More sharing options...
penguin0 Posted June 2, 2007 Share Posted June 2, 2007 give us the variable you use to store this "text stuff" then someone can creat an if statement. Quote Link to comment https://forums.phpfreaks.com/topic/53921-solved-create-active-links-automatically-using-php/#findComment-266603 Share on other sites More sharing options...
npsari Posted June 2, 2007 Author Share Posted June 2, 2007 Well, the data is inserted to DATABASE NAME: info TABLE NAME: users ROW NAME: description Is that what you mean Quote Link to comment https://forums.phpfreaks.com/topic/53921-solved-create-active-links-automatically-using-php/#findComment-266612 Share on other sites More sharing options...
AndyB Posted June 2, 2007 Share Posted June 2, 2007 http://www.phpfreaks.com/forums/index.php/topic,106159.msg424327.html#msg424327 Quote Link to comment https://forums.phpfreaks.com/topic/53921-solved-create-active-links-automatically-using-php/#findComment-266623 Share on other sites More sharing options...
npsari Posted June 2, 2007 Author Share Posted June 2, 2007 <?php $tests = array( 'http://www.mysite.com', 'http://mysite.com', 'www.mysite.com', 'http://mysite.com/wombats/stuff/show_me.php?this=6&that=7&furry=yes', 'text without an url', 'url with www.google.com in text', ); function clickable_short_url ($matches) { ### Entire match. $url = array_shift($matches); ### Only http:// match, if any. $http = array_shift($matches); ### Shorten textual part if need be. $url_text = strlen($url) >= 30 ? (substr($url, 0, 20) . '...' . substr($url, -10, 10)) : $url ; ### Add http:// prefix if need be. if (! $http) { $url = 'http://' . $url; } ### Return new url. return '<a target="_blank" href="' . $url . '">' . $url_text . '</a>'; } $pattern = '% (http://)? ### optional http:// prefix (?(1)|www\.) ### require www. if there is no http:// \S+ ### gobble anything but white space %x'; foreach ($tests as $test) { echo preg_replace_callback($pattern, 'clickable_short_url', $test); echo '<br/>'; } ?> Hi, the above code converts the array But what if I dont have a specific array I only have a colomb called: Description and I am using echo nl2br($row['Description']); to echo it So, how can i add the code above to it Quote Link to comment https://forums.phpfreaks.com/topic/53921-solved-create-active-links-automatically-using-php/#findComment-266839 Share on other sites More sharing options...
AndyB Posted June 2, 2007 Share Posted June 2, 2007 Just change echo nl2br($row['Description']); to echo nl2br(preg_replace_callback($pattern, 'clickable_short_url', $row['Description'])); Quote Link to comment https://forums.phpfreaks.com/topic/53921-solved-create-active-links-automatically-using-php/#findComment-266841 Share on other sites More sharing options...
npsari Posted June 2, 2007 Author Share Posted June 2, 2007 Hey thanks, it worked, it even reduces the size of the long links which is better looking Thanks for the hint and tip Quote Link to comment https://forums.phpfreaks.com/topic/53921-solved-create-active-links-automatically-using-php/#findComment-266859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.