CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BakeTask
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeRoute
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbConfigTask
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • FileLog
  • FixtureTask
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • MootoolsEngineHelper
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginShortRoute
  • PluginTask
  • ProjectTask
  • PrototypeEngineHelper
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • Set
  • Shell
  • String
  • TemplateTask
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  1: <?php
  2: /**
  3:  * Text Helper
  4:  *
  5:  * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
  6:  *
  7:  * PHP versions 4 and 5
  8:  *
  9:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 10:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  *
 12:  * Licensed under The MIT License
 13:  * Redistributions of files must retain the above copyright notice.
 14:  *
 15:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 16:  * @link          http://cakephp.org CakePHP(tm) Project
 17:  * @package       cake
 18:  * @subpackage    cake.cake.libs.view.helpers
 19:  * @since         CakePHP(tm) v 0.10.0.1076
 20:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 21:  */
 22: 
 23: /**
 24:  * Included libraries.
 25:  *
 26:  */
 27: if (!class_exists('HtmlHelper')) {
 28:     App::import('Helper', 'Html');
 29: }
 30: if (!class_exists('Multibyte')) {
 31:     App::import('Core', 'Multibyte');
 32: }
 33: 
 34: /**
 35:  * Text helper library.
 36:  *
 37:  * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
 38:  *
 39:  * @package       cake
 40:  * @subpackage    cake.cake.libs.view.helpers
 41:  * @link http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Text.html#Text
 42:  */
 43: class TextHelper extends AppHelper {
 44: 
 45: /**
 46:  * Highlights a given phrase in a text. You can specify any expression in highlighter that
 47:  * may include the \1 expression to include the $phrase found.
 48:  *
 49:  * ### Options:
 50:  *
 51:  * - `format` The piece of html with that the phrase will be highlighted
 52:  * - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
 53:  *
 54:  * @param string $text Text to search the phrase in
 55:  * @param string $phrase The phrase that will be searched
 56:  * @param array $options An array of html attributes and options.
 57:  * @return string The highlighted text
 58:  * @access public
 59:  * @link http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Text.html#Text#highlight-1622
 60:  */
 61:     function highlight($text, $phrase, $options = array()) {
 62:         if (empty($phrase)) {
 63:             return $text;
 64:         }
 65: 
 66:         $default = array(
 67:             'format' => '<span class="highlight">\1</span>',
 68:             'html' => false
 69:         );
 70:         $options = array_merge($default, $options);
 71:         extract($options);
 72: 
 73:         if (is_array($phrase)) {
 74:             $replace = array();
 75:             $with = array();
 76: 
 77:             foreach ($phrase as $key => $segment) {
 78:                 $segment = '(' . preg_quote($segment, '|') . ')';
 79:                 if ($html) {
 80:                     $segment = "(?![^<]+>)$segment(?![^<]+>)";
 81:                 }
 82: 
 83:                 $with[] = (is_array($format)) ? $format[$key] : $format;
 84:                 $replace[] = "|$segment|iu";
 85:             }
 86: 
 87:             return preg_replace($replace, $with, $text);
 88:         } else {
 89:             $phrase = '(' . preg_quote($phrase, '|') . ')';
 90:             if ($html) {
 91:                 $phrase = "(?![^<]+>)$phrase(?![^<]+>)";
 92:             }
 93: 
 94:             return preg_replace("|$phrase|iu", $format, $text);
 95:         }
 96:     }
 97: 
 98: /**
 99:  * Strips given text of all links (<a href=....)
100:  *
101:  * @param string $text Text
102:  * @return string The text without links
103:  * @access public
104:  * @link http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Text.html#Text#stripLinks-1623
105:  */
106:     function stripLinks($text) {
107:         return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
108:     }
109: 
110: /**
111:  * Adds links (<a href=....) to a given text, by finding text that begins with
112:  * strings like http:// and ftp://.
113:  *
114:  * @param string $text Text to add links to
115:  * @param array $options Array of HTML options.
116:  * @return string The text with links
117:  * @access public
118:  * @link http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Text.html#Text#autoLinkUrls-1619
119:  */
120:     function autoLinkUrls($text, $htmlOptions = array()) {
121:         $options = var_export($htmlOptions, true);
122:         $text = preg_replace_callback('#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i', create_function('$matches',
123:             '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text);
124: 
125:         return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
126:             create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . $matches[0],' . $options . ');'), $text);
127:     }
128: 
129: /**
130:  * Adds email links (<a href="mailto:....) to a given text.
131:  *
132:  * @param string $text Text
133:  * @param array $options Array of HTML options.
134:  * @return string The text with links
135:  * @access public
136:  * @link http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Text.html#Text#autoLinkEmails-1618
137:  */
138:     function autoLinkEmails($text, $options = array()) {
139:         $linkOptions = 'array(';
140:         foreach ($options as $option => $value) {
141:             $value = var_export($value, true);
142:             $linkOptions .= "'$option' => $value, ";
143:         }
144:         $linkOptions .= ')';
145:         $atom = '[a-z0-9!#$%&\'*+\/=?^_`{|}~-]';
146: 
147:         return preg_replace_callback(
148:             '/(' . $atom . '+(?:\.' . $atom . '+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)+)/i',
149:             create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $linkOptions . ');'), $text);
150:     }
151: 
152: /**
153:  * Convert all links and email adresses to HTML links.
154:  *
155:  * @param string $text Text
156:  * @param array $options Array of HTML options.
157:  * @return string The text with links
158:  * @access public
159:  * @link http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Text.html#Text#autoLink-1620
160:  */
161:     function autoLink($text, $options = array()) {
162:         return $this->autoLinkEmails($this->autoLinkUrls($text, $options), $options);
163:     }
164: 
165: /**
166:  * Truncates text.
167:  *
168:  * Cuts a string to the length of $length and replaces the last characters
169:  * with the ending if the text is longer than length.
170:  *
171:  * ### Options:
172:  *
173:  * - `ending` Will be used as Ending and appended to the trimmed string
174:  * - `exact` If false, $text will not be cut mid-word
175:  * - `html` If true, HTML tags would be handled correctly
176:  *
177:  * @param string  $text String to truncate.
178:  * @param integer $length Length of returned string, including ellipsis.
179:  * @param array $options An array of html attributes and options.
180:  * @return string Trimmed string.
181:  * @access public
182:  * @link http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Text.html#Text#truncate-1625
183:  */
184:     function truncate($text, $length = 100, $options = array()) {
185:         $default = array(
186:             'ending' => '...', 'exact' => true, 'html' => false
187:         );
188:         $options = array_merge($default, $options);
189:         extract($options);
190: 
191:         if ($html) {
192:             if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
193:                 return $text;
194:             }
195:             $totalLength = mb_strlen(strip_tags($ending));
196:             $openTags = array();
197:             $truncate = '';
198: 
199:             preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
200:             foreach ($tags as $tag) {
201:                 if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
202:                     if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
203:                         array_unshift($openTags, $tag[2]);
204:                     } else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
205:                         $pos = array_search($closeTag[1], $openTags);
206:                         if ($pos !== false) {
207:                             array_splice($openTags, $pos, 1);
208:                         }
209:                     }
210:                 }
211:                 $truncate .= $tag[1];
212: 
213:                 $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
214:                 if ($contentLength + $totalLength > $length) {
215:                     $left = $length - $totalLength;
216:                     $entitiesLength = 0;
217:                     if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
218:                         foreach ($entities[0] as $entity) {
219:                             if ($entity[1] + 1 - $entitiesLength <= $left) {
220:                                 $left--;
221:                                 $entitiesLength += mb_strlen($entity[0]);
222:                             } else {
223:                                 break;
224:                             }
225:                         }
226:                     }
227: 
228:                     $truncate .= mb_substr($tag[3], 0 , $left + $entitiesLength);
229:                     break;
230:                 } else {
231:                     $truncate .= $tag[3];
232:                     $totalLength += $contentLength;
233:                 }
234:                 if ($totalLength >= $length) {
235:                     break;
236:                 }
237:             }
238:         } else {
239:             if (mb_strlen($text) <= $length) {
240:                 return $text;
241:             } else {
242:                 $truncate = mb_substr($text, 0, $length - mb_strlen($ending));
243:             }
244:         }
245:         if (!$exact) {
246:             $spacepos = mb_strrpos($truncate, ' ');
247:             if (isset($spacepos)) {
248:                 if ($html) {
249:                     $bits = mb_substr($truncate, $spacepos);
250:                     preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
251:                     if (!empty($droppedTags)) {
252:                         foreach ($droppedTags as $closingTag) {
253:                             if (!in_array($closingTag[1], $openTags)) {
254:                                 array_unshift($openTags, $closingTag[1]);
255:                             }
256:                         }
257:                     }
258:                 }
259:                 $truncate = mb_substr($truncate, 0, $spacepos);
260:             }
261:         }
262:         $truncate .= $ending;
263: 
264:         if ($html) {
265:             foreach ($openTags as $tag) {
266:                 $truncate .= '</'.$tag.'>';
267:             }
268:         }
269: 
270:         return $truncate;
271:     }
272: 
273: /**
274:  * Extracts an excerpt from the text surrounding the phrase with a number of characters on each side
275:  * determined by radius.
276:  *
277:  * @param string $text String to search the phrase in
278:  * @param string $phrase Phrase that will be searched for
279:  * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
280:  * @param string $ending Ending that will be appended
281:  * @return string Modified string
282:  * @access public
283:  * @link http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Text.html#Text#excerpt-1621
284:  */
285:     function excerpt($text, $phrase, $radius = 100, $ending = '...') {
286:         if (empty($text) or empty($phrase)) {
287:             return $this->truncate($text, $radius * 2, array('ending' => $ending));
288:         }
289: 
290:         $append = $prepend = $ending;
291: 
292:         $phraseLen = mb_strlen($phrase);
293:         $textLen = mb_strlen($text);
294: 
295:         $pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
296:         if ($pos === false) {
297:             return mb_substr($text, 0, $radius) . $ending;
298:         }
299: 
300:         $startPos = $pos - $radius;
301:         if ($startPos <= 0) {
302:             $startPos = 0;
303:             $prepend = '';
304:         }
305: 
306:         $endPos = $pos + $phraseLen + $radius;
307:         if ($endPos >= $textLen) {
308:             $endPos = $textLen;
309:             $append = '';
310:         }
311: 
312:         $excerpt = mb_substr($text, $startPos, $endPos - $startPos);
313:         $excerpt = $prepend . $excerpt . $append;
314:         
315:         return $excerpt;
316:     }
317: 
318: /**
319:  * Creates a comma separated list where the last two items are joined with 'and', forming natural English
320:  *
321:  * @param array $list The list to be joined
322:  * @param string $and The word used to join the last and second last items together with. Defaults to 'and'
323:  * @param string $separator The separator used to join all othe other items together. Defaults to ', '
324:  * @return string The glued together string.
325:  * @access public
326:  * @link http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Text.html#Text#toList-1624
327:  */
328:     function toList($list, $and = 'and', $separator = ', ') {
329:         if (count($list) > 1) {
330:             return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
331:         } else {
332:             return array_pop($list);
333:         }
334:     }
335: }
336: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs