Return to Snippet

Revision: 2903
at May 3, 2007 08:44 by nicolaspar


Initial Code
function get_links($string) {
    $patron='/\<a href\=(.*?)\>(.*?)\<\/a\>/is';
    preg_match_all ($patron, $string,$coincidencias);
    return $coincidencias[0];
};
function get_links_text($string) {
    $patron='#[^<ref=">]http\:\/\/([^"<>]*)#is';
    preg_match_all ($patron, $string,$coincidencias);
    return $coincidencias[0];
    
};
function redlink($link,$largopartes){
    $patron='/\<a href\=(.*?)\>(.*?)\<\/a\>/i';
    preg_match_all ($patron, $link,$coincidencias);
    $s='/\<a href\=(.*?)\>(.*?)\<\/a\>/se';
    if(strlen($coincidencias[2][0])>($largopartes<<1))
        $r='"<a href=$1>".substr("$2",0,"$largopartes")."...".substr("$2","-$largopartes")."</a>"';
    else
        $r='"<a href=$1>$2</a>"';
    $t=preg_replace ($s, $r, $link);
    return $t;
}

function redlink2($link,$largopartes){
    if(strlen($link)>($largopartes<<1))
        $r='<a href="'.$link.'">'.substr($link,0,$largopartes).'...'.substr($link,-$largopartes).'</a>';
    else
        $r='<a href="'.$link.'">'.$link.'</a>';
        return $r;
}

$cadena='
um fringilla commodo, magna metus tempus justo, quis eleifend enim ante vel nisl. Nam tellus ligula, semper eu, pretium et, placerat sit amet, justo. Phasellus sagittis faucibus orci. Phasellus ultricies scelerisque nulla. Quisque metus. Aliquam leo dolor, pellentesque eu, cursus sollicitudin, porta a, velit.
hola http://www.akjhas.dasa/asdasda/asasa/asasa/asasasas/asas.pl <br><a href="http://www.google.com.ar">Este es un magnífico buscador y en él hay de todo</a>
 Nunc pulvinar. In porta nisl vitae magna. Nunc justo libero, feugiat at, dignissim sed, interdum eleifend, est. In ornare pellentesque odio. Ut mauris. Donec ornare vestibulum urna. Vestibulum et massa. Integer viverra, nisl ac congue elementum, nisi felis dignissim sapien, et adipiscing nisl urna a ligula. In mattis imperdiet dui. Fusce tincidunt mauris sed massa. Aenean at nisl. Sed enim ante, aliquam non, sagittis non, consequat et, dui. Aliquam faucibus adipiscing lacus. Praesent sem velit, scelerisque sit am
';
$links=get_links($cadena);
$urls=get_links_text($cadena);
foreach($links as $v){
    $replace[]=redlink($v,15);
}
foreach($urls as $v){
    $replace[]=redlink2($v,15);
}
$search=array_merge($links,$urls);

echo str_replace($search,$replace,$cadena);

Initial URL

                                

Initial Description

                                

Initial Title
PHP - Cortar link /url dentro de string

Initial Tags
url, link, text

Initial Language
PHP