Page 1 of 1
create links for urls and mail-links for email-adresses
Posted: Mon Dec 29, 2003 5:40 am
by vigge89
Can anyone tell me a working "URLirizer & EMAILirizer", two eregi_replace() scripts that create links from
http://www.domain.com text, and creating mailto:-links for email-adresses. The one i got right now doesn't work at all, it outputs "://" when the link should be
http://www.test.com and "" when there should be an email.
Code:
Code: Select all
<?php
$return1 = eregi_replace("([[]]+)://([^[]]*)([[]#?/&=])", "<a href='\1://\2\3' target='_blank'>\1://\2\3</a>", $string);
$string = eregi_replace("(([a-z0-9_]|\-|\.)+@([^[]]*)([[]-]))", "<a href='mailto:\1' target='_new'>\1</a>", $return1);
?>
Posted: Mon Dec 29, 2003 12:34 pm
by vigge89
anyone got some code ready?
i know that someone have, becuase lots of sites are using this "function", so please, share it!

Posted: Mon Dec 29, 2003 12:57 pm
by Derfel Cadarn
Hi Vigge,
I haven't got THAT code, but I use a simple function to check for each word, returned form a whois-lookup, wether it is an
e-mail@home.tv, a
http://thingy.com or simple text. It's based on the script a certain Matt wrote...
Here the function is:
Code: Select all
function insertlink($rawoutput) {
global $rawoutput;
// This function checks wether a word is an IP-number, a link or an e-mail-dress or not
// if so, it's presented as link to the whois-form
$rawlines = preg_split("/(?=\\n)/",$rawoutput);
$rawoutput = "";
foreach ($rawlines as $line){
$rawwords = preg_split("/(?=\s)/",$line);
$line = "";
foreach ($rawwords as $word) {
$word = trim($word);
$wrong_ip = false;
//echo "<br>[$word=".gettype($word)."]";
//echo "<br>$wrong_ip [".$word."]";
if(!preg_match('/^\d+\.\d+\.\d+\.\d+$/', $word)) {
$wrong_ip = true;
}
if (!$wrong_ip) {
$word = "<a class='text' href='ipwhois.php?domain=$word'>".$word."</a> ";
//echo "<a class='text' href='$thispage?domain=$word'>".$word."</a> ";
} elseif (preg_match("/@/",$word)) {
$word = "<a class='text' href='mailto:$word'>".$word."</a> ";
//echo "<a class='text' href='mailto:$word'>".$word."</a> ";
} elseif (preg_match("/^http:/",$word)) {
$word = "<a class='text' target='_new' href='$word'>".$word."</a> ";
//echo "<a class='text' target='new' href='$word'>".$word."</a> ";
} else {
$word = $word." ";
//echo $word." ";
}
$wrong_ip = false;
$line .= $word;
}
$line = $line."\n";
$rawoutput .= $line;
}
return($rawoutput);
}
I admit it isn't very clean, but it works great on my site!
Posted: Mon Dec 29, 2003 1:21 pm
by vigge89
thanks for that, but i would prefer the eregi_replace() code...
Posted: Mon Dec 29, 2003 2:34 pm
by Gen-ik
Code: Select all
<?php
$string = "Check out this website http://forums.devnetwork.net it's cool";
$string2 = "This is my email address me@home.com so contact me!";
// Replace HREF with a real link.
$search = "\shttp://([^\s]+)\s";
$replace = "<a href="http://\\1">\\1</a>";
$string = eregi_replace($search, $replace, $string);
// Replace Email with a real link.
$search = "\s([^@]+)@([^\s]+)\s";
$replace = "<a href="mailto:\\1@\\2">\\1@\\2</a>";
$string2 = eregi_replace($search, $replace, $string2);
echo $string."<br />".$string2;
?>
I think that will work... I've just coded it as I go so I haven't tested it.
Posted: Mon Dec 29, 2003 2:40 pm
by vigge89
Gen-Ik: that outputs
Code: Select all
Check out this website http://forums.devnetwork.net it's cool<br />Thi<a href="mailto: is my email address me@home.com "> is my email address me@home.com </a>o contact me!
for me

Posted: Mon Dec 29, 2003 2:48 pm
by Gen-ik
Hmmmmmm ok. Try this.
Code: Select all
<?php
$string = "Check out this website http://forums.devnetwork.net it's cool";
$string2 = "This is my email address me@home.com so contact me!";
// Replace HREF with a real link.
$search = "http://([^\s]+)";
$replace = "<a href="http://\\1">http://\\1</a> ";
$string = eregi_replace($search, $replace, $string);
// Replace Email with a real link.
$search = " ([^@]+)@([^\s]+) ";
$replace = " <a href="mailto:\\1@\\2">\\1@\\2</a> ";
$string2 = eregi_replace($search, $replace, $string2);
echo $string."<br />".$string2;
?>
If that doesn't work then check out
http://www.evolt.org/article/Regular_Ex ... index.html because my brain is too tired to work on eregi() at the mo.
Posted: Mon Dec 29, 2003 3:21 pm
by vigge89
Code: Select all
Check out this website <a href="http://forum">http://forum</a> s.devnetwork.net it's cool<br />This <a href="mailto:is my email address me@home.com">is my email address me@home.com</a> so contact me!

Posted: Mon Dec 29, 2003 3:30 pm
by vigge89
found a working URL-replacer @
http://se.php.net/manual/en/function.ereg-replace.php
Code: Select all
<?php
$search = "[[]]+://[^<>[]]+[[]/]";
$replace = "<a href="\\0">\\0</a>";
$string = eregi_replace($search, $replace, $string);
?>
But i'm still looking for a working email-linkmaker
