Friday, July 15, 2011

D6 converts URLs (http, ftp, email, ...) into hyperlinks.

$text field will contain the raw data which we need to convert into links.

$urlfilter_length = 72;
$text = $node_details->field_selling_external_prop_link[0]['value'];
$text = ' ' . $text . ' ';
$text = preg_replace_callback("!(

|

  • ||[ \n\r\t\(])((http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?]?)(?=(

    |
  • ||[ \n\r\t\)]))!i", 'urlfilter_replace1', $text);
    $text = preg_replace("!(

    |

  • ||[ \n\r\t\(])([A-Za-z0-9._-]+@[A-Za-z0-9._+-]+\.[A-Za-z]{2,4})([.,?]?)(?=(

    |
  • ||[ \n\r\t\)]))!i", '\1\2\3', $text);
    $text = preg_replace_callback("!(

    |

  • |[ \n\r\t\(])(www\.[a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+~#\&=/;-])([.,?]?)(?=(

    |
  • ||[ \n\r\t\)]))!i", 'urlfilter_replace2', $text);
    $text = substr($text, 1, -1);

    $text will contain the converted values.

    Define below 3 function
    function urlfilter_replace1($match) {
    $match[2] = decode_entities($match[2]);
    $caption = check_plain(urlfilter_trim($match[2]));
    $match[2] = check_url($match[2]);
    return $match[1] . ''. $caption .''. $match[5];
    }

    function urlfilter_replace2($match) {
    $match[2] = decode_entities($match[2]);
    $caption = check_plain(urlfilter_trim($match[2]));
    $match[2] = check_plain($match[2]);
    return $match[1] . ''. $caption .''. $match[3];
    }

    function urlfilter_trim($text) {
    $urlfilter_length = 72;
    return (strlen($text) > $urlfilter_length) ? substr($text, 0, $urlfilter_length) .'...' : $text;
    }

    No comments:

    Post a Comment