Convert url from a string to hyperlink

1

It works for several links but if I do the same 2x link in the string does not work how?

function MontarLink($texto)
{
       if (!is_string ($texto))
           return $texto;

    $er = "/(https:\/\/(www\.|.*?\/)?|http:\/\/(www\.|.*?\/)?|www\.)([a-zA-Z0-9]+|_|-)+(\.(([0-9a-zA-Z]|-|_|\/|\?|=|&)+))+/i";

    preg_match_all ($er, $texto, $match);

    foreach ($match[0] as $link)
    {

        //coloca o 'http://' caso o link não o possua
        $link_completo = (stristr($link, "http") === false) ? "http://" . $link : $link;

        $link_len = strlen ($link);

        //troca "&" por "&", tornando o link válido pela W3C
       $web_link = str_replace ("&", "&", $link_completo);
       $texto = str_ireplace ($link, "<a href=\"" . strtolower($web_link) . "\" target=\"_blank\">". (($link_len > 60) ? substr ($web_link, 0, 25). "...". substr ($web_link, -15) : $web_link) ."</a>", $texto);

    }

    return $texto;

}

echo MontarLink("ola mundo www.cade.com.br"); // ESSE FUNCIONA!!!
echo "<br><br>";
echo MontarLink("ola mundo www.cade.com.br outro site www.terra.com.br "); // ESSE FUNCIONA!!!
echo "<br><br>";
echo MontarLink("ola mundo www.cade.com.br mesmo site www.cade.com.br"); // NÃO FUNCIONA!!!
    
asked by anonymous 19.08.2014 / 20:57

2 answers

5

The problem is that when you find the first occurrence, you are replacing all of them, then the second occurrence is repeated and you have to replace it again, and it goes into a chain reaction, messing things up ...

Use the preg_replace_callback function:

function MontarLink($texto)
{
       if (!is_string ($texto))
           return $texto;

    $er = "/(https:\/\/(www\.|.*?\/)?|http:\/\/(www\.|.*?\/)?|www\.)([a-zA-Z0-9]+|_|-)+(\.(([0-9a-zA-Z]|-|_|\/|\?|=|&)+))+/i";

    $texto = preg_replace_callback($er, function($match){
        $link = $match[0];

        //coloca o 'http://' caso o link não o possua
        $link = (stristr($link, "http") === false) ? "http://" . $link : $link;

        //troca "&" por "&", tornando o link válido pela W3C
        $link = str_replace ("&", "&amp;", $link);

        return "<a href=\"" . strtolower($link) . "\" target=\"_blank\">". ((strlen($link) > 60) ? substr ($link, 0, 25). "...". substr ($link, -15) : $link) ."</a>";
    },$texto); 

    return $texto;

}

echo MontarLink("ola mundo www.cade.com.br"); // ESSE FUNCIONA!!!
echo "<br><br>";
echo MontarLink("ola mundo www.cade.com.br outro site www.terra.com.br "); // ESSE FUNCIONA!!!
echo "<br><br>";
echo MontarLink("ola mundo www.cade.com.br mesmo site www.cade.com.br"); // ESSE FUNCIONA!!!
    
19.08.2014 / 22:05
3

I researched a bit and found this answer in Stack Overflow that in addition to solving your problem of transforming text into links yet takes into account various particularities of a URL and still covers mailto links, if anyone still uses them.

The original response had some silly misconceptions which I fixed and will be making available here:

function makeClickableLinks( $text ) {

    $text = ' ' . html_entity_decode( $text );

    // Full-formed links

    $text = preg_replace(

        '#(((f|ht){1}tps?://)[-a-zA-Z0-9@:%_\+.~\#?&//=]+)#i',

        '<a href="\1" target=_blank>\1</a>',

        $text
    );

    // Links without scheme prefix (i.e. http://)

    $text = preg_replace(

        '#([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~\#?&//=]+)#i',

        '\1<a href="http://\2" target=_blank>\2</a>',

        $text
    );

    // E-mail links (mailto)

    $text = preg_replace(

        '#([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})#i',

        '<a href="mailto:\1" target=_blank>\1</a>',

        $text
    );

    return $text;
}

You can use it the same way you used it:

// Link com prefixo http:// e um mailto:

echo makeClickableLinks('

    This is a test clickable link: http://www.websewak.com  You can also try using an email address like [email protected]'
), '<br />';

// Links sem o prefixo http://

echo makeClickableLinks( 'www.cade.com.br' ), '<br />';

// Mais de um link no mesmo texto

echo makeClickableLinks(

    'ola mundo www.cade.com.br outro site www.terra.com.br'
), '<br />';

// Dois links iguais

echo makeClickableLinks(

    'ola mundo http://www.cade.com.br mesmo site http://www.cade.com.br'
), '<br />';
    
19.08.2014 / 22:30