/    Sign up×
Community /Pin to ProfileBookmark

I have this guestbook that I made. One of the fields is a home page field. On the display page I want to take the url they type and make it an actual link so that when you click on it you go to their site. I would also like to do that for their email. Here is the url so you can see what I’m trying to do.

[url]http://vulcan.resourceforge.com/~rtcstudent/mattdudley/madddisplay.php[/url]

my site:
[url]www.madddidley.com[/url]

[upl-file uuid=989df73c-ab6a-4bb9-96d6-cb5453f82761 size=7kB]girlpromo.jpg[/upl-file]

to post a comment
PHP

22 Comments(s)

Copy linkTweet thisAlerts:
@pyroMar 26.2004 — Show the PHP you use to display the URLs/email addresses, and someone can easily show you how to make them into links.
Copy linkTweet thisAlerts:
@JonaMar 26.2004 — [font=arial]Hello Madddidley,

You'd use Regular Expressions for that. Personally I wouldn't want the email to be a link, because of spam bots, but at any rate, you'd use a preg_replace function to replace the email address with a link--and the same goes for a site URL. Off the top of my head, here's an email one--untested:[/font]

[code=php]
preg_replace("/(.@\W\.[a-zA-z])/", "<a href="mailto:$1">$1</a>");
[/code]


[font=arial]You can most definitely find a much better one elsewhere, but this was an example. Pyro and Jeff are the experts at that kind of thing, so I'm sure one of them will come in here and show you something better. If not, well, I can always try again. ?

[b]Edit: Look at that! Pyro even beat me! :p [/b][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@pyroMar 26.2004 — [i]Originally posted by Jona [/i]

[B]here's an email one--untested:[/B][/QUOTE]
Might want to try testing that one. ?
Copy linkTweet thisAlerts:
@JonaMar 26.2004 — [i]Originally posted by pyro [/i]

[B]Might want to try testing that one. ? [/B][/QUOTE]


[code=php]
$email = preg_replace("/(.+@[a-zA-z]*.[a-zA-z]{3})/", "<a href="mailto:$1">$1</a>", $_POST["email"]);
[/code]


[font=arial]Am I getting close? :p [/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@madddidleyauthorMar 26.2004 — Thank for the help but I'm sorry, I'm not really to familar with php or mysql, just learning. here is my code.



Here is what is in the body of the page. Maybe if someone could modify it that would be great.

<?php


$query="select * from madddidley order by 'time'";

$result=mysql_query($query);

echo mysql_error();




while($e=mysql_fetch_array($result))

{

echo "

<div class="date">Time: ".$e['time']."</div>

<div class="date">Name: ".$e['name']."</div>

<div class="date">Email: ".$e['email']."</div>

<div class="date">Homepage: ".$e['homepage']."</div>

<div class="date">Comments: ".$e['comments']."</div>

<hr color=#FFFFFF width=100%>";

}

?>
Copy linkTweet thisAlerts:
@pyroMar 26.2004 — For simple checking, yes. There are still a few things, even for that, though. ?

[a-zA-z] should be [a-zA-[b]Z[/b]]

When using the quantifiers, you should probably use the quantifier minimizer (wow, that's a mouthfull), the ?, to make the matching ungreedy. Alternatly, you can use the 'U' flag to reverse the greediness of the quantifiers. In this case, using the ? would make them greedy.

Apart from that, you obviously have the issue that it would reject perfectly valid email addresses, due to way you are checking for the host. For instance, your example won't match .co.uk email addresses.

?
Copy linkTweet thisAlerts:
@pyroMar 26.2004 — You'll want something like this, then (though the code could be cleaned up a touch):

[code=php]<?php

$query="select * from madddidley order by 'time'";
$result=mysql_query($query);
echo mysql_error();

while($e=mysql_fetch_array($result))
{
echo "
<div class="date">Time: ".$e['time']."</div>
<div class="date">Name: ".$e['name']."</div>
<div class="date">Email: <a href="".$e['email']."">".$e['email']."</a></div>
<div class="date">Homepage: ".$e['homepage']."</div>
<div class="date">Comments: ".$e['comments']."</div>
<hr color=#FFFFFF width=100%>";
}
?>[/code]
Copy linkTweet thisAlerts:
@JonaMar 26.2004 — [i]Originally posted by pyro [/i]

[B][a-zA-z] should be [a-zA-[b]Z[/b]][/B][/QUOTE]


[font=arial]Holy lethargic sour dough sandwiches! I can't believe I did that! :p

OK, here's what I did now. Is it better? ? [/font]

[code=php]
$email = preg_replace("/(.+@[a-zA-Z]*(.[a-zA-Z]{2,}){1,})/", "<a href="mailto:$1">$1</a>", $_POST["email"]);
echo("Email: ". $email ."<br>");

$site = preg_replace("/((ht|ft)tp(s)?://(www.)?[a-zA-Z]+(.[a-zA-Z]{2,}){1,}(/)?)/", "<a href="$1">$1</a>", $_POST["site"]);
echo("Site: ". $site);
[/code]


[b][J]ona[/b]
Copy linkTweet thisAlerts:
@madddidleyauthorMar 26.2004 — Would I put this anywhere in the PHP

$site = preg_replace("/((ht|ft)tp(s)?://(www.)?[a-zA-Z]+(.[a-zA-Z]{2,}){1,}(/)?)/", "<a href="$1">$1</a>", $_POST["site"]);

and put this where i want it to display?

echo("Site: ". $site);
Copy linkTweet thisAlerts:
@JonaMar 26.2004 — [font=arial]Madddidly, it isn't necessary if you're using the method pyro helped you with, unless you want to validate the addresses.[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@madddidleyauthorMar 26.2004 — I'm sorry..............I don't understand
Copy linkTweet thisAlerts:
@JonaMar 26.2004 — [i]Originally posted by madddidley [/i]

[B]I'm sorry..............I don't understand [/B][/QUOTE]


[font=arial]Don't be sorry for what you don't understand--as long as you don't understand, you're among the innocent. ?

What I was saying is, the method pyro showed you should work fine for what you're doing. The method I was using was if you're using text files and are going to parse through the email address and homepage text strings and turn them into links through Regular Expressions. If the way pyro suggested works for you, then there's nothing else you need to do.

By the way, pyro, I updated the link RegEx:[/font]

[code=php]
$site = preg_replace("/((http|ftp)(s)?\:\/\/)?((www\.)?[a-zA-Z]+(\.[a-zA-Z]{2,}){1,}(\/)?)/", "<a href="http://$4">$1$4</a>", $_POST["site"]);
[/code]


[b][J]ona[/b]
Copy linkTweet thisAlerts:
@pyroMar 26.2004 — First, the email one: (and I'm not going to get nit-picky - they both should really be compaired against the appropriate RFCs, but I don't feel like taking the time to find the correct ones and verify it against it. I'll just go by what I know off the top of my head)

[list=1]
  • [*]You forgot about the quantifiers ? If you need an example, let me know.

  • [*]Domain names can contain more than just alphabetic characters. For instance, both digits, and the - are valid.

    [/list=1]

    Domain

    [list=1]
  • [*]Same issue of valid domain name characters

  • [*]The domains don't allow for subdomains, directories, or pages.

    [/list=1]

    That's a partial list, anyway. ?
  • Copy linkTweet thisAlerts:
    @JonaMar 26.2004 — [font=arial]Wow, um, okay. I'd like an example on quantifiers, please--biggie size. Could I get an order of "what do you mean by same issue of valid domain name characters" with that as well, please?[/font]

    [font=monospace]

    $site = preg_replace("/((http|ftp)(s)?://)?((www.)?[a-zA-Z(-)?]+(.[a-zA-Z]{2,}){1,}(/)?)/", "<a href="http://$4">$1$4</a>", $_POST["site"]);

    [/font]

    [b][J]ona[/b]
    Copy linkTweet thisAlerts:
    @pyroMar 26.2004 — Quantifier minimization:

    By default, quantifiers in PHP are greedy. Take this example:

    &lt;?PHP
    $str = '&amp;#91;b&amp;#93;this&amp;#91;/b&amp;#93; is &amp;#91;b&amp;#93;bold&amp;#91;/b&amp;#93;';
    $str = preg_replace("/[b](.*)[/b]/", "&lt;strong&gt;$1&lt;/strong&gt;", $str);
    echo $str;
    ?&gt;


    That will output [b]This is &#91;/b&#93; is &#91;b&#93;bold[/b]. Not the desired outcome, at all.

    So, if we use quantifier minimization, to make the * ungreedy, it will stop matching as soon as possible, in our case, at the [i]first[/i] &#91;/b&#93; rather than the last one.

    &lt;?PHP
    $str = '&amp;#91;b&amp;#93;this&amp;#91;/b&amp;#93; is &amp;#91;b&amp;#93;bold&amp;#91;/b&amp;#93;';
    $str = preg_replace("/[b](.*[color=firebrick][b]?[/b][/color])[/b]/", "&lt;strong&gt;$1&lt;/strong&gt;", $str);
    echo $str;
    ?&gt;


    Now we get the desired output: [b]this[/b] is [b]bold[/b]

    As far as the valid domain name characters go, you only allow [a-zA-Z], which is fine for http://www.ryanbrill.com/ but won't allow http://www.1976design.com/

    Poor Dunstan. ?
    Copy linkTweet thisAlerts:
    @JonaMar 26.2004 — [font=arial]Uh, and what does the bold example have to do with validating an email address?

    I put this for the domain. Is it better?[/font]

    [font=monospace]

    $site = preg_replace("/((http|ftp)(s)?://)?((www.)?([a-zA-z0-9(-)?]*.)?[a-zA-Z0-9(-)?]+(.[a-zA-Z]{2,}){1,}(/[a-zA-z0-9/.]*)?)/", "<a href="http://$4">$1$4</a>", $_POST["site"]);

    [/font]

    [b][J]ona[/b]
    Copy linkTweet thisAlerts:
    @pyroMar 26.2004 — [i]Originally posted by Jona [/i]

    [B]Uh, and what does the bold example have to do with validating an email address?[/B][/QUOTE]
    It has something to do with it, because in your case, you weren't "validating an email address" where you could (maybe!) assume that only one address would be passed. You were trying to parse a string and turn the addresses to links.
    Copy linkTweet thisAlerts:
    @JonaMar 26.2004 — [i]Originally posted by pyro [/i]

    [B]It has something to do with it, because in your case, you weren't "validating an email address" where you could (maybe!) assume that only one address would be passed. You were trying to parse a string and turn the addresses to links. [/B][/QUOTE]


    [font=arial]Oh I see. Leaving the technical aspect of it, can you explain to me what I would need that for in the email address? Just put a question mark after the asterisk?[/font]

    [b][J]ona[/b]
    Copy linkTweet thisAlerts:
    @madddidleyauthorMar 30.2004 — I could not figure it out the way you tried to help me. But I did get it to work. All i did was echo the <a href="http:// then I called that field from the database. you can check it out if you like.

    www.madddidley.com
    Copy linkTweet thisAlerts:
    @pyroMar 30.2004 — Like [url=http://forums.webdeveloper.com/showthread.php?s=&threadid=31211#post167920]this[/url], you mean? ?
    Copy linkTweet thisAlerts:
    @madddidleyauthorMar 30.2004 — hahahaha I feel so dumb. it was right there. dang
    Copy linkTweet thisAlerts:
    @pyroMar 30.2004 — hehe :p ?
    ×

    Success!

    Help @madddidley spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...