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.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 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
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • 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
  • write_css_cache
  1: <?php
  2: /* SVN FILE: $Id$ */
  3: /**
  4:  * String handling methods.
  5:  *
  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
 19:  * @since         CakePHP(tm) v 1.2.0.5551
 20:  * @version       $Revision$
 21:  * @modifiedby    $LastChangedBy$
 22:  * @lastmodified  $Date$
 23:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 24:  */
 25: /**
 26:  * String handling methods.
 27:  *
 28:  *
 29:  * @package       cake
 30:  * @subpackage    cake.cake.libs
 31:  */
 32: class String extends Object {
 33: /**
 34:  * Gets a reference to the String object instance
 35:  *
 36:  * @return object String instance
 37:  * @access public
 38:  * @static
 39:  */
 40:     function &getInstance() {
 41:         static $instance = array();
 42: 
 43:         if (!$instance) {
 44:             $instance[0] =& new String();
 45:         }
 46:         return $instance[0];
 47:     }
 48: /**
 49:  * Generate a random UUID
 50:  *
 51:  * @see http://www.ietf.org/rfc/rfc4122.txt
 52:  * @return RFC 4122 UUID
 53:  * @static
 54:  */
 55:     function uuid() {
 56:         $node = env('SERVER_ADDR');
 57:         $pid = null;
 58: 
 59:         if (strpos($node, ':') !== false) {
 60:             if (substr_count($node, '::')) {
 61:                 $node = str_replace('::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node);
 62:             }
 63:             $node = explode(':', $node) ;
 64:             $ipv6 = '' ;
 65: 
 66:             foreach ($node as $id) {
 67:                 $ipv6 .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT);
 68:             }
 69:             $node =  base_convert($ipv6, 2, 10);
 70: 
 71:             if (strlen($node) < 38) {
 72:                 $node = null;
 73:             } else {
 74:                 $node = crc32($node);
 75:             }
 76:         } elseif (empty($node)) {
 77:             $host = env('HOSTNAME');
 78: 
 79:             if (empty($host)) {
 80:                 $host = env('HOST');
 81:             }
 82: 
 83:             if (!empty($host)) {
 84:                 $ip = gethostbyname($host);
 85: 
 86:                 if ($ip === $host) {
 87:                     $node = crc32($host);
 88:                 } else {
 89:                     $node = ip2long($ip);
 90:                 }
 91:             }
 92:         } elseif ($node !== '127.0.0.1') {
 93:             $node = ip2long($node);
 94:         } else {
 95:             $node = null;
 96:         }
 97: 
 98:         if (empty($node)) {
 99:             $node = crc32(Configure::read('Security.salt'));
100:         }
101: 
102:         if (function_exists('zend_thread_id')) {
103:             $pid = zend_thread_id();
104:         } else {
105:             $pid = getmypid();
106:         }
107: 
108:         if (!$pid || $pid > 65535) {
109:             $pid = mt_rand(0, 0xfff) | 0x4000;
110:         }
111: 
112:         list($timeMid, $timeLow) = explode(' ', microtime());
113:         $uuid = sprintf("%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
114:                     mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node);
115: 
116:         return $uuid;
117:     }
118: /**
119:  * Tokenizes a string using $separator, ignoring any instance of $separator that appears between $leftBound
120:  * and $rightBound
121:  *
122:  * @param string $data The data to tokenize
123:  * @param string $separator The token to split the data on
124:  * @return array
125:  * @access public
126:  * @static
127:  */
128:     function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
129:         if (empty($data) || is_array($data)) {
130:             return $data;
131:         }
132: 
133:         $depth = 0;
134:         $offset = 0;
135:         $buffer = '';
136:         $results = array();
137:         $length = strlen($data);
138:         $open = false;
139: 
140:         while ($offset <= $length) {
141:             $tmpOffset = -1;
142:             $offsets = array(strpos($data, $separator, $offset), strpos($data, $leftBound, $offset), strpos($data, $rightBound, $offset));
143:             for ($i = 0; $i < 3; $i++) {
144:                 if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
145:                     $tmpOffset = $offsets[$i];
146:                 }
147:             }
148:             if ($tmpOffset !== -1) {
149:                 $buffer .= substr($data, $offset, ($tmpOffset - $offset));
150:                 if ($data{$tmpOffset} == $separator && $depth == 0) {
151:                     $results[] = $buffer;
152:                     $buffer = '';
153:                 } else {
154:                     $buffer .= $data{$tmpOffset};
155:                 }
156:                 if ($leftBound != $rightBound) {
157:                     if ($data{$tmpOffset} == $leftBound) {
158:                         $depth++;
159:                     }
160:                     if ($data{$tmpOffset} == $rightBound) {
161:                         $depth--;
162:                     }
163:                 } else {
164:                     if ($data{$tmpOffset} == $leftBound) {
165:                         if (!$open) {
166:                             $depth++;
167:                             $open = true;
168:                         } else {
169:                             $depth--;
170:                             $open = false;
171:                         }
172:                     }
173:                 }
174:                 $offset = ++$tmpOffset;
175:             } else {
176:                 $results[] = $buffer . substr($data, $offset);
177:                 $offset = $length + 1;
178:             }
179:         }
180:         if (empty($results) && !empty($buffer)) {
181:             $results[] = $buffer;
182:         }
183: 
184:         if (!empty($results)) {
185:             $data = array_map('trim', $results);
186:         } else {
187:             $data = array();
188:         }
189:         return $data;
190:     }
191: /**
192:  * Replaces variable placeholders inside a $str with any given $data. Each key in the $data array corresponds to a variable
193:  * placeholder name in $str. Example:
194:  *
195:  * Sample: String::insert('My name is :name and I am :age years old.', array('name' => 'Bob', '65'));
196:  * Returns: My name is Bob and I am 65 years old.
197:  *
198:  * Available $options are:
199:  *  before: The character or string in front of the name of the variable placeholder (Defaults to ':')
200:  *  after: The character or string after the name of the variable placeholder (Defaults to null)
201:  *  escape: The character or string used to escape the before character / string (Defaults to '\')
202:  *  format: A regex to use for matching variable placeholders. Default is: '/(?<!\\)\:%s/' (Overwrites before, after, breaks escape / clean)
203:  *  clean: A boolean or array with instructions for String::cleanInsert
204:  *
205:  * @param string $str A string containing variable placeholders
206:  * @param string $data A key => val array where each key stands for a placeholder variable name to be replaced with val
207:  * @param string $options An array of options, see description above
208:  * @return string
209:  * @access public
210:  * @static
211:  */
212:     function insert($str, $data, $options = array()) {
213:         $defaults = array(
214:             'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
215:         );
216:         $options += $defaults;
217:         $format = $options['format'];
218: 
219:         if (!isset($format)) {
220:             $format = sprintf(
221:                 '/(?<!%s)%s%%s%s/',
222:                 preg_quote($options['escape'], '/'),
223:                 str_replace('%', '%%', preg_quote($options['before'], '/')),
224:                 str_replace('%', '%%', preg_quote($options['after'], '/'))
225:             );
226:         }
227:         if (!is_array($data)) {
228:             $data = array($data);
229:         }
230: 
231:         if (array_keys($data) === array_keys(array_values($data))) {
232:             $offset = 0;
233:             while (($pos = strpos($str, '?', $offset)) !== false) {
234:                 $val = array_shift($data);
235:                 $offset = $pos + strlen($val);
236:                 $str = substr_replace($str, $val, $pos, 1);
237:             }
238:         } else {
239:             asort($data);
240: 
241:             $hashKeys = array_map('md5', array_keys($data));
242:             $tempData = array_combine(array_keys($data), array_values($hashKeys));
243:             krsort($tempData);
244:             foreach ($tempData as $key => $hashVal) {
245:                 $key = sprintf($format, preg_quote($key, '/'));
246:                 $str = preg_replace($key, $hashVal, $str);
247:             }
248:             $dataReplacements = array_combine($hashKeys, array_values($data));
249:             foreach ($dataReplacements as $tmpHash => $data) {
250:                 $str = str_replace($tmpHash, $data, $str);
251:             }
252:         }
253: 
254:         if (!isset($options['format']) && isset($options['before'])) {
255:             $str = str_replace($options['escape'].$options['before'], $options['before'], $str);
256:         }
257:         if (!$options['clean']) {
258:             return $str;
259:         }
260:         return String::cleanInsert($str, $options);
261:     }
262: /**
263:  * Cleans up a Set::insert formated string with given $options depending on the 'clean' key in $options. The default method used is
264:  * text but html is also available. The goal of this function is to replace all whitespace and uneeded markup around placeholders
265:  * that did not get replaced by Set::insert.
266:  *
267:  * @param string $str
268:  * @param string $options
269:  * @return string
270:  * @access public
271:  * @static
272:  */
273:     function cleanInsert($str, $options) {
274:         $clean = $options['clean'];
275:         if (!$clean) {
276:             return $str;
277:         }
278:         if ($clean === true) {
279:             $clean = array('method' => 'text');
280:         }
281:         if (!is_array($clean)) {
282:             $clean = array('method' => $options['clean']);
283:         }
284:         switch ($clean['method']) {
285:             case 'html':
286:                 $clean = array_merge(array(
287:                     'word' => '[\w,.]+',
288:                     'andText' => true,
289:                     'replacement' => '',
290:                 ), $clean);
291:                 $kleenex = sprintf(
292:                     '/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
293:                     preg_quote($options['before'], '/'),
294:                     $clean['word'],
295:                     preg_quote($options['after'], '/')
296:                 );
297:                 $str = preg_replace($kleenex, $clean['replacement'], $str);
298:                 if ($clean['andText']) {
299:                     $options['clean'] = array('method' => 'text');
300:                     $str = String::cleanInsert($str, $options);
301:                 }
302:                 break;
303:             case 'text':
304:                 $clean = array_merge(array(
305:                     'word' => '[\w,.]+',
306:                     'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
307:                     'replacement' => '',
308:                 ), $clean);
309: 
310:                 $kleenex = sprintf(
311:                     '/(%s%s%s%s|%s%s%s%s)/',
312:                     preg_quote($options['before'], '/'),
313:                     $clean['word'],
314:                     preg_quote($options['after'], '/'),
315:                     $clean['gap'],
316:                     $clean['gap'],
317:                     preg_quote($options['before'], '/'),
318:                     $clean['word'],
319:                     preg_quote($options['after'], '/')
320:                 );
321:                 $str = preg_replace($kleenex, $clean['replacement'], $str);
322:                 break;
323:         }
324:         return $str;
325:     }
326: }
327: ?>
328: 
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