1
function link_it($text)
{
    $text= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" target=\"_blank\">$3</a>", $text);  
    $text= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" target=\"_blank\">$3</a>", $text);  
    $text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\" target=\"_blank\">$2@$3</a>", $text);  
    return($text);  
}

That's the working code.

I'm working on a new function

function shorturl2full($url)
{
    echo 'URL IS: ' . $url;
    return "FULLLINK";
}

The idea is to take the url and return it back. Later going to work on turning it in to the full url. So like t.co will be full url they will see.

$text= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" target=\"_blank\">" .  shorturl2full("$3") . "</a>", $text);  
        $text= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" target=\"_blank\">" .  shorturl2full("$3") . "</a>", $text);  
        $text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\" target=\"_blank\">$2@$3</a>", $text);  
        return($text);  
}

Is my bad try at it.

So if you click the link it should use the original but the one you see should be the output of shorturl2full

So like <a href="t.co">FULLLINK</a>

I want to attempt to write the shorturl2full function on my own and i think i have a very great idea on how to do it. The problem is in the link_it function... It needs to pass the url to the shorturl2full function and display what ever it returned.

2 Answers 2

2
+50

You can use preg_replace_callback instead of preg_replace http://nz.php.net/manual/en/function.preg-replace-callback.php

function link_it($text)
{
    $text= preg_replace_callback("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is",  'shorturl2full', $text);  
    $text= preg_replace_callback("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is",  'shorturl2full', $text);  
    $text= preg_replace_callback("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i",  'shorturl2full', $text);  
    return($text);  
}

function shorturl2full($url)
{
    $fullLink = 'FULLLINK';
    // $url[0] is the complete match
    //... you code to find the full link
    return '<a href="' . $url[0] . '">' . $fullLink . '</a>';
}

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0

In a previous answer I have shown a function called make_clickable which has an optional callback parameter which get's applied to each URI if set:

make_clickable($text, 'shorturl2full');

Maybe it's helpful or gives some ideas at least.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.