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 me@domain.com 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

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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: me@mydomain.com

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

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.