Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Limit long words

Here you are… ported from wp-chunk (literally only had to change the filter names)

<?php
/*
Plugin Name: bb-chunk
Description: Shortens the display of urls so they won't break your site theme. Ported from wp-chunk 2.0 ( http://www.village-idiot.org/archives/2006/06/29/wp-chunk/ )
Author: The How-To Geek
Author URI: http://www.howtogeek.com
Version: 0.1
*/

function make_chunky($ret)
{

// pad it with a space
$ret = ' ' . $ret;
$ret = preg_replace("#(^|[n ])([w]+?://[w#$%&~/.-;:=,?@[]+]*)#is", "$1<a href='$2' rel='nofollow'>$2</a>", $ret);
$ret = preg_replace("#(^|[n ])((www|ftp).[w#$%&~/.-;:=,?@[]+]*)#is", "$1<a href='http://$2' rel='nofollow'>$2</a>", $ret);
//chunk those long urls
chunk_url($ret);
$ret = preg_replace("#(s)([a-z0-9-_.]+)@([^,< nr]+)#i", "$1<a href="mailto:$2@$3">$2@$3</a>", $ret);
// Remove our padding..
$ret = substr($ret, 1);
return($ret);
}

function chunk_url(&$ret)
{

$links = explode('<a', $ret);
$countlinks = count($links);
for ($i = 0; $i < $countlinks; $i++)
{
$link = $links[$i];

$link = (preg_match('#(.*)(href=")#is', $link)) ? '<a' . $link : $link;

$begin = strpos($link, '>') + 1;
$end = strpos($link, '<', $begin);
$length = $end - $begin;
$urlname = substr($link, $begin, $length);

/**
* We chunk urls that are longer than 50 characters. Just change
* '50' to a value that suits your taste. We are not chunking the link
* text unless if begins with 'http://', 'ftp://', or 'www.'
*/
$chunked = (strlen($urlname) > 50 && preg_match('#^(http://|ftp://|www.)#is', $urlname)) ? substr_replace($urlname, '.....', 30, -10) : $urlname;
$ret = str_replace('>' . $urlname . '<', '>' . $chunked . '<', $ret);

}
}

remove_filter('post_text', 'make_clickable');
add_filter('post_text', 'make_chunky');
?>

Skip to toolbar