Jump to content

Auto Link function problem


AtomicRax

Recommended Posts

I found a function online to auto link any variable containing text. It looks for http:// or www. in the text and then automatically adds the HTML code to turn it into a link. I have a problem though.. I have some html already in some of the variables that contain IMG tags. This function tries to link the url of the image.. so I get something like this:

 

<img src="<a rel="nofollow" href="IMAGEURL" target="_blank">IMAGEURL</a>" border="0" />

 

This creates a problem and the images don't load! Is there anyway to stop it from doing this?

 

function auto_link($text) {
    $pattern  = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
    return preg_replace_callback($pattern, 'auto_link_callback', $text);
}

function auto_link_callback($matches) {
    $max_url_length = 50;
    $max_depth_if_over_length = 2;
    $ellipsis = '…';

    $url_full = $matches[0];
    $url_short = '';

    if (strlen($url_full) > $max_url_length) {
        $parts = parse_url($url_full);
        $url_short = $parts['scheme'] . '://' . preg_replace('/^www\./', '', $parts['host']) . '/';

        $path_components = explode('/', trim($parts['path'], '/'));
        foreach ($path_components as $dir) {
            $url_string_components[] = $dir . '/';
        }

        if (!empty($parts['query'])) {
            $url_string_components[] = '?' . $parts['query'];
        }

        if (!empty($parts['fragment'])) {
            $url_string_components[] = '#' . $parts['fragment'];
        }

        for ($k = 0; $k < count($url_string_components); $k++) {
            $curr_component = $url_string_components[$k];
            if ($k >= $max_depth_if_over_length || strlen($url_short) + strlen($curr_component) > $max_url_length) {
                if ($k == 0 && strlen($url_short) < $max_url_length) {
                    // Always show a portion of first directory
                    $url_short .= substr($curr_component, 0, $max_url_length - strlen($url_short));
                }
                $url_short .= $ellipsis;
                break;
            }
            $url_short .= $curr_component;
        }

    } else {
        $url_short = $url_full;
    }


    return "<a rel=\"nofollow\" href=\"$url_full\" target=\"_blank\">$url_short</a>";
}

Link to comment
https://forums.phpfreaks.com/topic/227419-auto-link-function-problem/
Share on other sites

Well I've found a fix for my problem. The only images that were in the code were images from my servers. I already have an array of each of my image storage server addresses, so I simply compare the address being parsed against my array and if it matches a record, it returns the URL untouched. If not, the URL is linked.

 

I replaced the return in auto_link_callback() with this:

 

	foreach ($server as $URLCheck) {
	if ($parts['host'] == $URLCheck['server']) { return $url_full; }
	else { return "<a rel=\"nofollow\" href=\"$url_full\" target=\"_blank\">$url_short</a>"; }
}

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.