Jump to content

Turning a url or email into a link automatically


alexcmm

Recommended Posts

<?php

  if (isset($_POST['link'])){
    echo '<a href="'.$_POST['url'].'">'.$_POST['url'].'</a>';
  }

echo '<form action="" method="POST">
Link URL: <input type="text" name="url">
<input type="submit" name="link" value="Create Link">
</form>';
?>

 

Is that what you are talking about?

No, lets say some one types in a form:

 

"If you need any help, contact me at [email protected]"

 

which is then submitted to my database... Is there a way to have that "[email protected]" turn into

<a href="mailto:[email protected]">[email protected]</a>

so that it displays as a link automatically on the html page.

 

EDIT: YEA, like they're doing here on this page!!

Of course there is a way, I think it has something to do with finding any @ signs in the post text, then working backwards to the previous space to give you the mail name, then working forward to get the domain, of course, this would be a very sketchy way of doing it, becuase someone could type an @ sign without meaning it to be an address, so idk.  I'll look at the phpbb code and see what I can find.

Yea halo2 is right, the best thing to do is find the location of an @ sign with substr. (it gives an index of the @. Go backwards until a space is had and forwards until a space is had. Once you have the string verify that it is an email address with some type of email verification function and if it is than str_replace the old email with the html email code.

 

--FrosT

Maybe you could use a preg_replace, maybe:

 

$string = "Email me: [email protected]";
preg_replace("|(.+?)@(.+?)|", "<a href=\"" . $1 . "\">" . $2 . "</a>", $string);

 

Maybe, I'll test it.

 

Edit: That doesn't work, though I was on the right track, I tried instead a preg_match, this:

 

<?php
$str = "Email me: [email protected] fdsaf";
//preg_replace("|(.*)@(.*)|", "<a href=\"mailto:" . ${1} . "\">" . ${2} . "</a>", $str);
preg_match("|@(.*).com|", $str, $match);
print_r($match);
?>

 

And it returns this:

 

Array ( [0] => Email me: [email protected] [1] => E [2] => mail me: bob [3] => bob.com )

 

So as you can see, I can get the domain, just not the username, basically this is searching for this:

 

@ANYTHING.com, so as long as the domain suffix is a .com, you have half of it.

Ok, I'm a total newb at this stuff... I've tried my best to understand how to turn an array into something usable, but I'm just not savy enough.

 

I'm trying to turn this:

$ud_weekly = nl2br($ud_weekly);

 

which could have text with an email in it somewhere, into text with an active email address in it.

 

Can you help an idiot get from where I am to what you've got?

I don't mean to sound like a jerk, but... I think I'm using it for the only purpose it has...

 

If someone types:

 

Hey Alex,
What's going on? How's your day?

My day is going great!

Email me is you want, [email protected]

Later

 

I don't want it all to be on one line... I want line breaks when it shows back up in HTML. The other side... when/if they edit this, I strip the HTML out before showing it in the form with this code:

$ud_weekly = strip_tags($ud_weekly);

 

Does this really matter? All that seems to work fine, I just want the "[email protected]" to show up as an active link automatically like it does here.

 

I'm still new to this, but it seems like your missing the point by focusing on something outside of my question. If you'de like you disregard my previous code and just say I'm working with:

$ud_weekly=mysql_result($result,$i,"weekly");

 

... if it helps.

 

I really do appreciate your input, just frustrating when something inconsequential gets in the way of my real issue.

 

Thanks!

To create line breaks from that, use:

 

$text = str_replace("\n", "<br />", $text);

 

You could also use \n\r, instead of just \n, idk what the difference really is though.

 

Thats beside the point though.  You will need to use preg_match (actually, replace, but I don't know how that works), match something like this:

 

preg_match("|(.+?)@(.+?).(???)|", $text, $match);

print_r($match);

 

Replace (???) with a regular expression that would match a typical domain suffix (.com, .net, .info)

 

This will print an array with anything matching *@*.* which would almost certainly be an email address, or course, there are complications, it would get anything before the *@ also, meaning that if you had a sentence like this:

 

my email is [email protected], thank you.

 

You would get this result:

 

my email is [email protected]

 

And not just the [email protected], something would have to be done about that.  Sorry I can't be more help.

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.