4images Forum & Community

4images Modifications / Modifikationen => Mods & Plugins (Requests & Discussions) => Topic started by: TerraZone on October 02, 2009, 02:25:18 PM

Title: Cut category description after x words
Post by: TerraZone 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?
Title: Re: Cut category description after x words
Post by: impss 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}
Title: Re: Cut category description after x words
Post by: TerraZone 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 ;)
Title: Re: Cut category description after x words
Post by: V@no 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.
Title: Re: Cut category description after x words
Post by: impss on October 06, 2009, 05:38:40 PM
Sorry about that, forgot to change that.

My post above is updated
Title: Re: Cut category description after x words
Post by: V@no 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 ;)
Title: Re: Cut category description after x words
Post by: AKIN on October 15, 2009, 03:10:41 PM
hi
how do {image_name_short} ?
which change code?
Title: Re: Cut category description after x words
Post by: V@no on October 15, 2009, 03:16:07 PM
category_bit.html
Title: Re: Cut category description after x words
Post by: AKIN on October 15, 2009, 03:17:05 PM
image name:  this picture in istanbul
image_name_shor:      this picture

how do :)
Title: Re: Cut category description after x words
Post by: V@no 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/
Title: Re: Cut category description after x words
Post by: AKIN 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;   
    }   
}
Title: Re: Cut category description after x words
Post by: V@no on October 15, 2009, 04:02:02 PM
too early for me  :oops:...code fixed.
Title: Re: Cut category description after x words
Post by: CodeMan on April 20, 2013, 01:58:53 AM
V@no,

Can this also be done for image description?

Thanks!