Jump to content

Help ! i want to identify url written in text area and display it hyperlinked.


garrisonian14

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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....

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.