Jump to content

[SOLVED] Making a Clickable Link from a Variable


JSHINER

Recommended Posts

<?php

$content = "This is the content with a link: http://www.link.com - visit the site!"

echo $content;

?>

 

How can I make it so if someone enters a link it makes the address is clickable? And if they only enter www, it includes http:// in the href.

A quick glance at the php.net manual reveals this:

 

<?php
function hyperlink(&$text)
{
    // match protocol://address/path/
    $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text);

    // match www.something
    $text = ereg_replace("(^| |.)(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text);
}
?>

 

Source: http://uk2.php.net/manual/en/function.ereg-replace.php#75448

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.