Jump to content

Automatically hyperlink links and email addresses entered in a textarea?


izbryte

Recommended Posts

you could do something like this:

 

<?php
$string = "here is a string of text including [email protected] an e-mail address and http://www.somesite.com a web address";

$newString = "";

$mailArray = explode(" ",$string);

foreach ($mailArray as $word){

if(ereg('^([-a-zA-Z0-9.]+@[-a-zA-Z0-9]+(\.[-a-zA-Z0-9]+)+)*$', $word)){
  $newString.= "<a href=\"mailto:".$word."\">".$word."</a> ";
}elseif(substr($word,0,4) == "http"){
  $newString.= "<a href=\"".$word."\">".$word."</a> ";
}else{
  $newString.= $word." ";
}

}

echo $newString;

?>

 

EDIT: FIXED

 

Thank you but I'm still a bit confused. Your code worked great when I tried it out as is but when I pull the text from the database it doesn't show the link. Am I doing something wrong?

 

Here's my code;

$contact = stripslashes(nl2br($row['contact']));
			 $newString = "";
			$mailArray = explode(" ",$contact);
			foreach ($mailArray as $word){

			 if(ereg('^([-a-zA-Z0-9.]+@[-a-zA-Z0-9]+(\.[-a-zA-Z0-9]+)+)*$', $word)){
			  $newString.= "<a href=\"mailto:".$word."\">".$word."</a> ";
			 }else{
			  $newString.= $word." ";
			 }
			}

			echo $newString;

thanks so much for helping me but it still isn't working. do you think it could have something to do with the text?

Here is an example of what is being pulled from the database:

Please send resumes to:

My Compnay

Jane Doe

12345 My Avenue, Los Angeles, CA 12345

Email: [email protected]

Phone: (123) 123-12345 | Fax: (123) 123-12345

Archived

This topic is now archived and is closed to further replies.

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