Keyword

Characters Not Words

  • Mongo
  • Mongo's Avatar Topic Author
  • Offline
  • Junior Member
More
14 years 2 months ago #87159 by Mongo
Characters Not Words was created by Mongo
I know this isn't a big thing, but you know how something just gets your gonads going!..that is if you have gonads...sorry digressed.PROBLEM:In the settings for the categories it gives an option to limit the words to be displayed with the picture and article stats. Well I'm a designer first and a coder second, I was wondering if there was a part of the code I could edit to change the limiting of words to characters.  This would really be good to even up the images on the listing table. I'll dig in the code of K2 for the rest of the night to see if I can find it. If not I would appreciate any clues you K2er's may have.

Please Log in or Create an account to join the conversation.

  • Mongo
  • Mongo's Avatar Topic Author
  • Offline
  • Junior Member
More
14 years 2 months ago #87160 by Mongo
Replied by Mongo on topic Characters Not Words
ok I found:

if ($item->params->get('catItemIntroTextWordLimit') && $task=='category'){
$item->introtext = K2HelperUtilities::wordLimit($item->introtext, $item->params->get('catItemIntroTextWordLimit'));
}

and


function wordLimit($str, $limit = 100, $end_char = '…') {
if (trim($str) == '')
return $str;
// always strip tags for text
$str = strip_tags($str);
preg_match('/\s*(?:\S*\s*){'.(int) $limit.'}/', $str, $matches);
if (strlen($matches[0]) == strlen($str))
$end_char = '';
return rtrim($matches[0]).$end_char;
}

but they don't seem to react to charLimit, is there another way of doing it that I don't know of?

Please Log in or Create an account to join the conversation.

  • Mongo
  • Mongo's Avatar Topic Author
  • Offline
  • Junior Member
More
14 years 2 months ago #87161 by Mongo
Replied by Mongo on topic Characters Not Words
Almost a month with no reply for such a simple issue.. can I get a bit of help to point me in the right direction?

Please Log in or Create an account to join the conversation.

More
14 years 2 months ago #87162 by David R.
Replied by David R. on topic Characters Not Words
I'm not a huge fan of changing the internals, nor have I looked at your issue in any depth but based on your analysis, simply stripping out the preg_match... etc stuff and substituting something like:

// if you want this to work with utf-8 you'd need to us mb_strlen() and mb_substr

if (trim(strlen($str)) > $limit) {
return $str;
} else {
return substr($str, 0, $limit).$end_char;
}

This will cut in the middle of words, takes no account of the length of the '...' etc, but you could easily tweak it further.

Just to make it less of a maintenance issue on future releases, I would probably make my own function for this, comment out the original wordLimit function and have the customized wordLimit wrap my character length function.


Mongo said:Almost a month with no reply for such a simple issue.. can I get a bit of help to point me in the right direction?

Please Log in or Create an account to join the conversation.

  • Mongo
  • Mongo's Avatar Topic Author
  • Offline
  • Junior Member
More
14 years 2 months ago #87163 by Mongo
Replied by Mongo on topic Characters Not Words
David thank you for replying bro, but I had gotten it worked a little earlier, I was able to keep everything in tact so there was no missing K2 Junctions.

// Word limit
function wordLimit($str, $limit = 100, $end_char = '…') {
if (trim($str) == '')
return $str;
// always strip tags for text
$str = strip_tags($str);
preg_match('/\s*(?:\S*\s*){'.(int) $limit.'}/', $str, $matches);
if (strlen($matches[0]) == strlen($str))
$end_char = '';
return substr($str, 0, $limit).$end_char;
}


will change words to chars.. works like a charm.

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum