How to limit text Output?

Hi, I'm using printImageDesc() to display the Image description just under the thumbnails in the gallery view, but I'd like to limit the output to a certain fixed limit number of characters and add a link to the image item. Just like some News items. I know this shouldn't be difficult and I've tried something unsuccesfully but I'm not finding an example (that I can understand! ;-) in the docs ...
That's my code now:

        <h5><?php printBareImageTitle(); ?></h5>
        <?php echo printImageDesc(); ?>

Comments

  • acrylian Administrator, Developer
    edited May 2022

    Use this to get the plain desc:
    https://docs.zenphoto.org/1.5.x/function-getImageDesc.html
    That contains HTML formatting. There is also: https://docs.zenphoto.org/1.5.x/function-getBareImageDesc.html

    Or the object model if there is a current image:
    $_zp_current_image->getDesc();

    Rhen use https://docs.zenphoto.org/1.5.x/function-truncate_string.html to shortend the text. Use this with the "bare" version.

    To keep formatting of HTML better use this as that try to fix invalid html (to work properly your server PHP needs the the tidy class installed):
    https://docs.zenphoto.org/1.5.x/function-shortenContent.html

    add a link to the image item

    The iamge item would be the single image page.
    https://docs.zenphoto.org/1.5.x/function-getImageURL.html
    You should see that on your theme's album.php

    Or the object model if there is a current image:
    $_zp_current_image->getLink();

    The object model version would be

    If you mean the original full image itself:https://docs.zenphoto.org/1.5.x/function-getFullImageURL.html

    Or the object model if there is a current image:
    $_zp_current_image->getFullImageURL();

  • kyrd Member

    Thankyou for your help, I'll try to make it work in my site :-)

  • kyrd Member

    I post here what I did, just in case someone else is interested

    <?php echo shortenContent($_zp_current_image->getDesc(),222, ' <b>(...)</b> '); ?>
    

    after unsuccesfully trying to insert a dynamic link to the ImageURL by wrapping the above <b>(...)</b> I decided to add instead the following lines

    <a class="" href="<?php echo html_encode(getImageURL()); ?>" title="<?php echo html_encode(getBareImageTitle());?>">
    <span class="" style="display:block;white-space:nowrap;">[ <b>&rArr;</b> ]</span></a>
    

    (Obviously the styles and classes are a working draft)

    However I made this choice mainly because I've noticed that shortenContent() very cleverly closes any opened tag eventually interrupted (i.e. <a href ... >, <b>, <i> ...).

    Here is a working example: http://www.indirizzofantasma.net/bnv/str-art/

    A further step would be to print the link only when the shortenContent() is used, but I don't know how to write the if statement...

  • acrylian Administrator, Developer

    However I made this choice mainly because I've noticed that shortenContent() very cleverly closes any opened tag eventually interrupted

    Yes, "fixing" broken HTML is the main purpose of this. For plain string truncation you can just use native PHP functions.

    A further step would be to print the link only when the shortenContent() is used, but I don't know how to write the if statement...

    Rather basic native PHP actually. just check the length of the description to be larger than your expected length using strlen. Of course that will also count HTML elements.

  • kyrd Member

    mmm, yeah, so it would mean that to make it work properly one should scrap away the unwanted HTML before counting... surely someone who knows howto PHP can make it easily.
    In my case this is exactly a Murphy's law rule: Every solution brings new problems
    ;-)

  • acrylian Administrator, Developer
    edited May 2022

    Stripping html is rather easy using either our getBare() function or even the nativ PHP function strip_tags().

Sign In or Register to comment.