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?

Link to comment
Share on other sites

No, lets say some one types in a form:

 

"If you need any help, contact me at bob@bob.com"

 

which is then submitted to my database... Is there a way to have that "bob@bob.com" turn into

<a href="mailto:bob@bob.com">bob@bob.com</a>

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

 

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Maybe you could use a preg_replace, maybe:

 

$string = "Email me: bob@bob.com";
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: bob@bob.com 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: bob@bob.com [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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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, bob@bob.com

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 "bob@bob.com" 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!

Link to comment
Share on other sites

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 bob@bob.com, thank you.

 

You would get this result:

 

my email is bob@bob.com

 

And not just the bob@bob.com, something would have to be done about that.  Sorry I can't be more help.

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.