Truncate at space, Zend View Helper

By David Marland
· 99 words ( )

Reading speed

Don't know your reading speed?
Start a timer while reading this post. When finished, stop the timer to find your reading speed.

When writing web applications you may frequently find the need to truncate strings. In php you could just use substr but that could be dangerous.

Therefore the safest idea is to truncate back to the nearest space. The code below is a Zend View Helper, constructed in a way to read cleanly in the page. It’s use in a Zend View is as follows:

<?$string = 'Hello People';?>
<?=$this->truncate($string)->toLength(10)?>
<?=$this->truncate($string)->toLength(10)->withPostfix('--')?>
<?=$this->truncate($string)->midword()->toLength(10)->withPostfix('--')?>

As each method returns itself they can be chained together. The above outputs;

Hello…
Hello--
Hello peop--

Methods

<?php
class Zend_View_Helper_Truncate extends Zend_View_Helper_Abstract {
        private $_string;
        private $_length;
        private $_postfix;
        private $_cutatspace = true;

        public function truncate($string) {
            $this->_string = trim($string);
            $this->_defaultValues();
            return $this;
        }

        private function _defaultValues() {
            $this->toLength(100);
            $this->withPostfix('&#0133;');
         }

        public function midword() {
            $this->_cutatspace = false;
            return $this;
        }

        public function toLength($int) {
            $this->_length = (int) $int;
            return $this;
        }
        public function withPostfix($str) {
            $this->_postfix = $str;
            return $this;
        }

        public function render() {
            // Return empty string if max length < 1
            if ($this->_length < 1) {
                return '';
            }

            // Return full string if max length >= string length
            if ($this->_length >= strlen($this->_string)) {
                return $this->_string;
            }

            // Return truncated string
            if ($this->_cutatspace) {
                while (strlen($this->_string) > $this->_length) {
                    $cutPos = strrpos($this->_string, ' ', -1);
                    if ($cutPos === false) {
                        // no spaces left, whole string truncated
                        return '';
                    }
                    $this->_string = trim(substr($this->_string, 0, $cutPos));
                }
            } else {
                $this->_string = trim(substr($this->_string, 0, $this->_length));
            }
            return $this->_string . $this->_postfix;
        }

        public function __toString() {
            return $this->render();
        }
}

Update 11/02/2012: Steve noted an issue with this helper. I have updated the code above based on his suggestion. Thanks Steve.

Stop reading timer

Elapsed time: 00:00:00

© 2011 - 2026 hammerspace.co.uk / Piko design system / Admin

The opinions expressed on this site are my own and do not necessarily reflect the views of my employers, past or present.

Look Ma, no cookie banner. Anything stored in your browser is purely functional. No information is stored about you or shared with anyone. Your IP address may be used to estimate your location to see which regions generate the most traffic, but is not stored after that estimation. A cookie may be set to prevent double counting of page views, but is not used for tracking (which is acceptable without a cookie banner under the UK's DUAA and EU Digital Omnibus Package).
This site is proxied via Cloudflare, which may collect some information about your visit. See Cloudflare's privacy policy.