Jump to content

Automatic Hyperlinking


mpridham

Recommended Posts

(Sorry for the double post. I just realized I posted it in the math forum by mistake)

 

I have created a very simple blog application and would like to add the functionality of having hyperlinks automatically created when a url is entered. For instance, when I post a blog entry with something like "Go to www.foobar.com", I would like it to automatically add the anchor tags (like this site does.)

 

Any ideas?

 

Thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/140234-automatic-hyperlinking/
Share on other sites

[tex]never noticed this tag whats this do?[/tex]

 

^^ a personal test lol.. but besides that let me write up something

 

but you have to study some regex its very neat :P

 

/((?:www\.|https?:\/\/)[.a-z0-9;&?\/-]+?)/i

thats really really simple but should do your job (havn't tested it)

 

--------------

wowie that tex thing is sweet lol

<?php
$text = "omg hey go to http://www.lovinglori.com/signatures.php www.google.com its so cool!";
function link($a,$b) {
	if ($a == 'http://' || $a == "https://") {
		return "<a href='{$b}'>$b</a>";
	}
	else {
		return "<a href='http://{$b}'>$b</a>";
	}
}
echo preg_replace("/((www\.|https?:\/\/)[.a-z0-9;&?\/-]+)/ie","link('\\2','\\1')",$text);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.