How to print the bare contents of a textObject?

Hey

I have some text in a .txt file which using class-textobject (or class-anyfile?) I treat as an image. How do I print the bare contents of this text file?
I tried many functions and only printCustomSizedImage* work, but they don't return bare contents.
I used: `<?php printCustomSizedImage(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, false, NULL); ?>`
This returns: `This is the content of foo.txt`

I would like pure text returned, nothing else. Is this possible?

Comments

  • acrylian Administrator, Developer
    You will have to use standard PHP function like `file_get_contents()` to read and display the plain file. Which is what the plugin uses to get it as well. See the `getContent()` method of it. You could also use `getContent()` and strip the wrapping span via `strip_tags`.
  • And one more question, is it possible to print just the image filename without anything else? Like getUnprotectedImageURL minus the paths and minus the extension.
  • Ooh thank you, I got it working! Though I don't know if what I did is either correct or efficient :) Is this the best way to do it?
    `<?php $txtContents = $_zp_current_image->getContent(); $txtContents = strip_tags($txtContents); echo $txtContents; ?>`
  • I managed to get the full path without the extension using `<?php $txtFilename = getUnprotectedImageURL(); $txtFilename = stripSuffix($txtFilename); echo $txtFilename; ?>` which returns
    /zp/albums/foo/bar/kitteh
    Now how do I get rid of "/zp/albums/foo/bar/"?
  • acrylian Administrator, Developer
    `$txtFilename = str_replace(WEBPATH,'',$txtFilename)` should work. We have several of these constants like `WEBPATH`. I guess I should make a user guide entry about these some time.
  • That one just removes "/zp".
  • acrylian Administrator, Developer
    Try `WEBPATH.'/'.ALBUMFOLDER.'/'.$_zp_current_album->name';
  • Success!
    `<?php $txtFilename = getUnprotectedImageURL(); $txtFilename = str_replace(WEBPATH.'/'.ALBUMFOLDER.'/'.$_zp_current_album->name.'/','',$txtFilename); $txtFilename = stripSuffix($txtFilename); echo $txtFilename; ?>`
    Thank you!
  • acrylian Administrator, Developer
    Btw, of course `echo $_zp_current_image->filename` gets you the file name even easier. Textobjects are descendents of the image class so a lot works on them like on images. Sorry, forgot to mention earlier.
  • For anyone interested, the pathinfo and stripsuffix functions works great for getting parts of a path or filename!

    pathinfo returns an array consisting of dirname, basename and extension.
    stripsuffix does as the name implies.

    To get the txt filename:
    `$txtFilename = pathinfo(getUnprotectedImageURL()); $txtFilename = $txtFilename ['basename'];`
    To strip the suffix:
    `$txtFilename = stripSuffix($txtFilename);`

    You could shorten and merge the two:
    `$txtFilename = pathinfo(getUnprotectedImageURL()); $txtFilename = stripSuffix($txtFilename ['basename']);`
  • acrylian Administrator, Developer
    Isn't what I told above much easier to get the filename? For other parts of paths there are several path related constants like WEBPATH, ALBUMFOLDER etc.
  • I used the code you gave me above for some time, but once things started getting hairy I found this to be quicker to experiment with. Both have uses, hope this helps someone too :)
Sign In or Register to comment.