valdeir2000

StackOverflow 266968 - Text.php

Jan 4th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.43 KB | None | 0 0
  1. <?php
  2. /**
  3.  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4.  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5.  *
  6.  * Licensed under The MIT License
  7.  * For full copyright and license information, please see the LICENSE.txt
  8.  * Redistributions of files must retain the above copyright notice.
  9.  *
  10.  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11.  * @link          https://cakephp.org CakePHP(tm) Project
  12.  * @since         1.2.0
  13.  * @license       https://opensource.org/licenses/mit-license.php MIT License
  14.  */
  15. namespace Cake\Utility;
  16.  
  17. use InvalidArgumentException;
  18.  
  19. /**
  20.  * Text handling methods.
  21.  */
  22. class Text
  23. {
  24.  
  25.     /**
  26.      * Tokenizes a string using $separator, ignoring any instance of $separator that appears between
  27.      * $leftBound and $rightBound.
  28.      *
  29.      * @param string $data The data to tokenize.
  30.      * @param string $separator The token to split the data on.
  31.      * @param string $leftBound The left boundary to ignore separators in.
  32.      * @param string $rightBound The right boundary to ignore separators in.
  33.      * @return array|string Array of tokens in $data or original input if empty.
  34.      */
  35.     public static function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')')
  36.     {
  37.         if (empty($data)) {
  38.             return [];
  39.         }
  40.  
  41.         $depth = 0;
  42.         $offset = 0;
  43.         $buffer = '';
  44.         $results = [];
  45.         $length = mb_strlen($data);
  46.         $open = false;
  47.  
  48.         while ($offset <= $length) {
  49.             $tmpOffset = -1;
  50.             $offsets = [
  51.                 mb_strpos($data, $separator, $offset),
  52.                 mb_strpos($data, $leftBound, $offset),
  53.                 mb_strpos($data, $rightBound, $offset)
  54.             ];
  55.             for ($i = 0; $i < 3; $i++) {
  56.                 if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
  57.                     $tmpOffset = $offsets[$i];
  58.                 }
  59.             }
  60.             if ($tmpOffset !== -1) {
  61.                 $buffer .= mb_substr($data, $offset, $tmpOffset - $offset);
  62.                 $char = mb_substr($data, $tmpOffset, 1);
  63.                 if (!$depth && $char === $separator) {
  64.                     $results[] = $buffer;
  65.                     $buffer = '';
  66.                 } else {
  67.                     $buffer .= $char;
  68.                 }
  69.                 if ($leftBound !== $rightBound) {
  70.                     if ($char === $leftBound) {
  71.                         $depth++;
  72.                     }
  73.                     if ($char === $rightBound) {
  74.                         $depth--;
  75.                     }
  76.                 } else {
  77.                     if ($char === $leftBound) {
  78.                         if (!$open) {
  79.                             $depth++;
  80.                             $open = true;
  81.                         } else {
  82.                             $depth--;
  83.                             $open = false;
  84.                         }
  85.                     }
  86.                 }
  87.                 $offset = ++$tmpOffset;
  88.             } else {
  89.                 $results[] = $buffer . mb_substr($data, $offset);
  90.                 $offset = $length + 1;
  91.             }
  92.         }
  93.         if (empty($results) && !empty($buffer)) {
  94.             $results[] = $buffer;
  95.         }
  96.  
  97.         if (!empty($results)) {
  98.             return array_map('trim', $results);
  99.         }
  100.  
  101.         return [];
  102.     }
  103. }
Add Comment
Please, Sign In to add comment