alexcmm Posted March 15, 2007 Share Posted March 15, 2007 Hey there, Is there an easy way to automatically create a link from a url or email address typed into a form? Thanks, ~Alex Quote Link to comment Share on other sites More sharing options...
Chevy Posted March 15, 2007 Share Posted March 15, 2007 <?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? Quote Link to comment Share on other sites More sharing options...
alexcmm Posted March 15, 2007 Author Share Posted March 15, 2007 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!! Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted March 15, 2007 Share Posted March 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
alexcmm Posted March 15, 2007 Author Share Posted March 15, 2007 Do you have an example of what this might look like? Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted March 15, 2007 Share Posted March 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
alexcmm Posted March 16, 2007 Author Share Posted March 16, 2007 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? Quote Link to comment Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 I do not understand why you are using nl2br, that converts newline breaks to <br /> ....now I'm completely confused. Quote Link to comment Share on other sites More sharing options...
alexcmm Posted March 16, 2007 Author Share Posted March 16, 2007 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! Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted March 17, 2007 Share Posted March 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Kasuke_Akira Posted March 17, 2007 Share Posted March 17, 2007 preg_replace() works like this: $output = preg_replace($search_for, $replace_with, $in_this_string); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.