Author Topic: Cut category description after x words  (Read 15794 times)

0 Members and 1 Guest are viewing this topic.

Offline TerraZone

  • Pre-Newbie
  • Posts: 7
    • View Profile
Cut category description after x words
« on: October 02, 2009, 02:25:18 PM »
Hey there,

is it possible to "cut" the category-description in the category overview, after a few words?
I have some really long descriptions that won't fit into my list. 
So it would be great if just the beginning is shown and the rest be replaced by ". . . " or something like that.

Any idea of how to do this?

Offline impss

  • Sr. Member
  • ****
  • Posts: 382
    • View Profile
    • Cusstom.net
Re: Cut category description after x words
« Reply #1 on: October 02, 2009, 04:09:32 PM »
Give this a try, it is untested

In /includes/functions.php

Find:
Code: [Select]
"cat_description" => format_text($cat_cache[$category_id]['cat_description'], 1),
After add:
Code: [Select]
"cat_description_short" => (strlen($cat_cache[$category_id]['cat_description']) > 20) ? Cut_String($cat_cache[$category_id]['cat_description'], 20)."..." : $cat_cache[$category_id]['cat_description'],
***In the above code the 20 represents the max length of description in characters, you can change this (twice)  to fit your need ***

Above:
Code: [Select]
?>
Add:
Code: [Select]
function Cut_String($string, $max_length){  
    if (strlen($string) > $max_length){  
        $string = substr($string, 0, $max_length);  
        $pos = strrpos($string, " ");  
        if($pos === false) {  
                return substr($string, 0, $max_length)."...";  
        }  
            return substr($string, 0, $pos)."...";  
    }else{  
        return $string;  
    }  
}

In your category_bit.html

Find:
Code: [Select]
{cat_description}
Replace with:
Code: [Select]
{cat_description_short}
« Last Edit: October 06, 2009, 05:36:13 PM by impss »

Offline TerraZone

  • Pre-Newbie
  • Posts: 7
    • View Profile
Re: Cut category description after x words
« Reply #2 on: October 06, 2009, 04:13:20 PM »
thanks for your help, but there seem to be a bug in your code.  after changing everything, the description for the first category ist displayed for EVERY category.  But it IS cut ;)

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Administrator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Cut category description after x words
« Reply #3 on: October 06, 2009, 04:23:45 PM »
Code: [Select]
"cat_description_short" => Cut_String($cat_cache[$category_id]['cat_description'], 20),should do it.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline impss

  • Sr. Member
  • ****
  • Posts: 382
    • View Profile
    • Cusstom.net
Re: Cut category description after x words
« Reply #4 on: October 06, 2009, 05:38:40 PM »
Sorry about that, forgot to change that.

My post above is updated

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Administrator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Cut category description after x words
« Reply #5 on: October 07, 2009, 02:36:20 AM »
@impss:
Code: [Select]
(strlen($cat_cache[$category_id]['cat_description']) > 20) is not needed, as it's already implemented into the new function ;)
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline AKIN

  • Full Member
  • ***
  • Posts: 121
    • View Profile
    • Resim
Re: Cut category description after x words
« Reply #6 on: October 15, 2009, 03:10:41 PM »
hi
how do {image_name_short} ?
which change code?

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Administrator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Cut category description after x words
« Reply #7 on: October 15, 2009, 03:16:07 PM »
category_bit.html
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline AKIN

  • Full Member
  • ***
  • Posts: 121
    • View Profile
    • Resim
Re: Cut category description after x words
« Reply #8 on: October 15, 2009, 03:17:05 PM »
image name:  this picture in istanbul
image_name_shor:      this picture

how do :)

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Administrator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Cut category description after x words
« Reply #9 on: October 15, 2009, 03:38:08 PM »
Oh, completely misunderstood your question. Sorry.

Same deal, in includes/functions.php find:
    "image_name" => format_text($image_row['image_name'], 2),


insert below:
    "image_name_short" => Cut_string(format_text($image_row['image_name'], 2), 20),


[EDIT]

Just found a nice code snippet that should only count and cut text, disregarding HTML tags, and supposedly should not break HTML in the text either:
http://www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags/
« Last Edit: October 15, 2009, 04:01:23 PM by V@no »
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline AKIN

  • Full Member
  • ***
  • Posts: 121
    • View Profile
    • Resim
Re: Cut category description after x words
« Reply #10 on: October 15, 2009, 03:57:44 PM »
Warning: Missing argument 2 for Cut_String(), called in C:\wamp\www\includes\functions.php on line 454 and defined in C:\wamp\www\includes\functions.php on line 1811

Warning: Missing argument 2 for Cut_String(), called in C:\wamp\www\includes\functions.php on line 454 and defined in C:\wamp\www\includes\functions.php on line 1811

Warning: Missing argument 2 for Cut_String(), called in C:\wamp\www\includes\functions.php on line 454 and defined in C:\wamp\www\includes\functions.php on line 1811

Warning: Missing argument 2 for Cut_String(), called in C:\wamp\www\includes\functions.php on line 454 and defined in C:\wamp\www\includes\functions.php on line 1811



in 454 line:     "image_name_short" => Cut_string(format_text($image_row['image_name'], 2)),
in 1811 line:    function Cut_String($string, $max_length){       if (strlen($string) > $max_length){   
        $string = substr($string, 0, $max_length);   
        $pos = strrpos($string, " ");   
        if($pos === false) {   
                return substr($string, 0, $max_length)."...";   
        }   
            return substr($string, 0, $pos)."...";   
    }else{   
        return $string;   
    }   
}

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Administrator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Cut category description after x words
« Reply #11 on: October 15, 2009, 04:02:02 PM »
too early for me  :oops:...code fixed.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline CodeMan

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • ArabGlobe.com
Re: Cut category description after x words
« Reply #12 on: April 20, 2013, 01:58:53 AM »
V@no,

Can this also be done for image description?

Thanks!