create links for urls and mail-links for email-adresses

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

create links for urls and mail-links for email-adresses

Post 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);
?>
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

anyone got some code ready?
i know that someone have, becuase lots of sites are using this "function", so please, share it! :!: :D
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post 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!
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

thanks for that, but i would prefer the eregi_replace() code...
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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!
:(
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :?
Post Reply