garrisonian14 Posted November 21, 2007 Share Posted November 21, 2007 Hi, I am using html textarea control. user writes text in text area and i save it in database using php, then i display it where ever it is required. I want if user writes some url in the area like www.abc.com or http://www.abc.com , When i display this text as output this url must be shown there as hyperlink. by clicking over it user must be redirected to www.abc .com Is there any html textarea control property that can do it, or i need to write some php or javascript code for it. If someone has php script for it please help. It is urgent. Thanx Ali. Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 21, 2007 Share Posted November 21, 2007 You will need to use regular expressions. Heres ya go: function hyperlink($text) { $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text); // this will match protocol://address/path/ $text = ereg_replace("(^| |.)(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text); // this will match www.something return $text; } And use it like so: $textarea = hyperlink($textarea); That should work for ya, ive not tested it though Quote Link to comment Share on other sites More sharing options...
garrisonian14 Posted November 22, 2007 Author Share Posted November 22, 2007 You will need to use regular expressions. Heres ya go: function hyperlink($text) { $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text); // this will match protocol://address/path/ $text = ereg_replace("(^| |.)(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text); // this will match www.something return $text; } And use it like so: $textarea = hyperlink($textarea); That should work for ya, ive not tested it though Thanx a lot for your help. It works fine when we have www.abc.com. But have one problem When there is http://www.abc.com then its out put is : www.abc.com">http://www.abc.com as it replaces the string twice. first for http:// match and then for www match. I have not worled over regular expressions that''s why i am unable to fix this problem. Kindly please fix this problem for me. It will be a great favor from your side. Thanx again. Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 just add the 2 new lines after { for example function hyperlink($text) { $lala = explode("http://", $text); $text = $lala[1]; . . . Quote Link to comment Share on other sites More sharing options...
garrisonian14 Posted November 22, 2007 Author Share Posted November 22, 2007 just add the 2 new lines after { for example function hyperlink($text) { $lala = explode("http://", $text); $text = $lala[1]; . . . But problem is as told earlier that input string will not be just http://www.abc.com . It will be content of a textarea. It may be link, Hello friend, My url is http://www.abc.com and i have many other urls too www.xyz.com . in this case when you explode with http:// and then it will not generate right output string. Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 exactly. in any case the http://www.abc.com will turn to www.abc.com which its link again... of course you continue with the code from wes1890. I said add not replace.. sorry if that doesnt satisfy you but my knowledge is limited... i write php just a week Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 ok... i know what you mean. I just tested it the link is coming www.mysite.com/www.abc.com I dont know how to bit this error, im working on it, if you find the solution please post it Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 but check it... i think it works because wes1980 adds http:// later in his code... so its correct what i said.. Quote Link to comment Share on other sites More sharing options...
garrisonian14 Posted November 22, 2007 Author Share Posted November 22, 2007 exactly. in any case the http://www.abc.com will turn to www.abc.com which its link again... of course you continue with the code from wes1890. I said add not replace.. sorry if that doesnt satisfy you but my knowledge is limited... i write php just a week I have used it now this one function hyperlink($text) { $output_string = ""; $lala = explode("http://", $text); for($i=0; $i<count($lala); $i++) { $text = $lala[$i]; $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text); // this will match protocol://address/path/ $text = ereg_replace("(^| |.)(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text); // this will match www.something $output_string .= $text; } return $output_string; } it is working except in the case of http://abc.com now just this problem is left. Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 22, 2007 Share Posted November 22, 2007 Found this at the PHP manual: (modified for you) <?php function hyperlink ($string) { $string = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is', '\\1<a href="\\2">\\2</a>', $string); $string = preg_replace('#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is', '\\1<a href="http://\\2">\\2</a>', $string); $string = preg_replace('#(^|\s)(([a-z0-9._%+-]+)@(([.-]?[a-z0-9])*))#is', '\\1<a href="mailto:\\2">\\2</a>', $string); return $string; } ?> It seems to work for all examples I've tried. PS I posted the code here --> http://code-bin.homedns.org/63 for permanent reference. Quote Link to comment Share on other sites More sharing options...
garrisonian14 Posted November 22, 2007 Author Share Posted November 22, 2007 Found this at the PHP manual: (modified for you) <?php function hyperlink ($string) { $string = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is', '\\1<a href="\\2">\\2</a>', $string); $string = preg_replace('#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is', '\\1<a href="http://\\2">\\2</a>', $string); $string = preg_replace('#(^|\s)(([a-z0-9._%+-]+)@(([.-]?[a-z0-9])*))#is', '\\1<a href="mailto:\\2">\\2</a>', $string); return $string; } ?> It seems to work for all examples I've tried. PS I posted the code here --> http://code-bin.homedns.org/63 for permanent reference. Thanx friend, it is great. Working for me. So nice of you. Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 22, 2007 Share Posted November 22, 2007 Glad to help. It helps you, me, and any other people who see it! Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 for me as well.. It worths of mention that when I tried to print it like <a href="<?php echo $textarea; ?>"></a> of course outside of php the result was www.abc.com> to work needs just echo or print.. Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 22, 2007 Share Posted November 22, 2007 stephen (pksml) nice code..... must also point out to all regular exsperesion users that preg_replace regex is the way foward becouse from php 6 the eregi function is turned off and preg function is defualt be warned....... why i tell you this becouse if u get a job in php your be confused why you can not use eregi in the future and trust me getting a free host or hoster to change there php.ini is very hard.... this is only advise from me so learn preg regex oposed to eregi regex if you need to learn regex use a program called http://www.regexbuddy.com/ very good.... Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 22, 2007 Share Posted November 22, 2007 stephen (pksml) nice code..... must also point out to all regular exsperesion users that preg_replace regex is the way foward becouse from php 6 the eregi function is turned off and preg function is defualt be warned....... why i tell you this becouse if u get a job in php your be confused why you can not use eregi in the future and trust me getting a free host or hoster to change there php.ini is very hard.... this is only advise from me so learn preg regex oposed to eregi regex if you need to learn regex use a program called http://www.regexbuddy.com/ very good.... Thanks for that bit of news (and the compliment:). I use http://www.snapfiles.com/get/regexcoach.html for Regex help. You might give it a try. Best of all, it's freeware! Quote Link to comment 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.