License Public Domain
Lines 22
Keywords
excerpt (1) php (3) substring (1)
Permissions
Owner: Björn
Viewable by Everyone
Editable by All Siafoo Users
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

Get excerpt from string Atom Feed 0

# 's
 1<?php
2
3/**
4 * Get excerpt from string
5 *
6 * @param String $str String to get an excerpt from
7 * @param Integer $startPos Position int string to start excerpt from
8 * @param Integer $maxLength Maximum length the excerpt may be
9 * @return String excerpt
10 */
11function getExcerpt($str, $startPos=0, $maxLength=100) {
12 if(strlen($str) > $maxLength) {
13 $excerpt = substr($str, $startPos, $maxLength-3);
14 $lastSpace = strrpos($excerpt, ' ');
15 $excerpt = substr($excerpt, 0, $lastSpace);
16 $excerpt .= '...';
17 } else {
18 $excerpt = $str;
19 }
20
21 return $excerpt;
22}
23
24?>