![]() |
|
How to limit text Output? - Printable Version +- ZenphotoCMS Forum (https://forum.zenphoto.org) +-- Forum: Support (https://forum.zenphoto.org/forum-1.html) +--- Forum: General support (https://forum.zenphoto.org/forum-4.html) +--- Thread: How to limit text Output? (/thread-13723.html) |
How to limit text Output? - kyrd - 2022-05-06 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: <?php printBareImageTitle(); ?> <?php echo printImageDesc(); ?> How to limit text Output? - acrylian - 2022-05-06 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()`; How to limit text Output? - kyrd - 2022-05-06 Thankyou for your help, I'll try to make it work in my site :-) How to limit text Output? - kyrd - 2022-05-09 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());?>"> [ <b>⇒</b> ]</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... How to limit text Output? - acrylian - 2022-05-09 > 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. How to limit text Output? - kyrd - 2022-05-09 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_ ;-) How to limit text Output? - acrylian - 2022-05-09 Stripping html is rather easy using either our `getBare()` function or even the nativ PHP function `strip_tags()`. |