Zend Framework Source Code Snippets

Word-based substring

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
<?php
/**
 * Takes a string and optionally a maximum length and 'cut' the string to match that length, based on words and not on characters.
 * @author Er Galvao Abbott
 *  * @param string  $pStr    The string to be cutted
 * @param intefer $pMaxLen The maximum length to be used to cut the string
 * 
 * @return string
 */ 
class Zend_View_Helper_StringCutter extends Zend_View_Helper_Abstract
{
    public function stringCutter($pStr, $pMaxLen = 40)
    {        if (strlen($pStr) > $pMaxLen) {
            $returnStr = '';
            $pieces = preg_split('/b/', $pStr);
            
            foreach ($pieces as $piece) {                if (strlen($returnStr . $piece) < $pMaxLen) {
                    $returnStr .= $piece;
                }
            }
                        $returnStr .= ' ...';
        } else {
            $returnStr = $pStr;
        }
                return $returnStr;
    }
}
?>

Comments

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Creates a substring of a given string, respecting word boundaries.

Snippet details

Created:
galvao galvao
2 years ago
Edited:
galvao galvao
2 years ago
Revision Id:
14
Edit Message:
Initial Release
Tags:
strings substring
Comments:
0
Views:
1171
Points:
1 (1 votes)

History

r14

Initial Release

galvao galvao
2 years ago