Utility.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. <?php
  2. namespace Tools\Utility;
  3. use Cake\Routing\Router;
  4. use Cake\Utility\Hash;
  5. use RuntimeException;
  6. /**
  7. * Main class for all app-wide utility methods
  8. *
  9. * @author Mark Scherer
  10. * @license MIT
  11. */
  12. class Utility {
  13. /**
  14. * More sane !empty() method to not false positive `'0'` (0 as string) as empty.
  15. *
  16. * @param mixed $value
  17. * @return bool
  18. */
  19. public static function notEmpty($value) {
  20. return !empty($value) || $value === '0';
  21. }
  22. /**
  23. * Clean implementation of inArray to avoid false positives.
  24. *
  25. * in_array itself has some PHP flaws regarding cross-type comparison:
  26. * - in_array('50x', array(40, 50, 60)) would be true!
  27. * - in_array(50, array('40x', '50x', '60x')) would be true!
  28. *
  29. * @param mixed $needle
  30. * @param array $haystack
  31. * @return bool Success
  32. */
  33. public static function inArray($needle, $haystack) {
  34. $strict = !is_numeric($needle);
  35. return in_array((string)$needle, $haystack, $strict);
  36. }
  37. /**
  38. * Tokenizes a string using $separator.
  39. *
  40. * Options
  41. * - clean: true/false (defaults to true and removes empty tokens and whitespace)
  42. *
  43. * @param string $data The data to tokenize
  44. * @param string $separator The token to split the data on.
  45. * @param array $options
  46. * @return array
  47. */
  48. public static function tokenize($data, $separator = ',', $options = []) {
  49. $defaults = [
  50. 'clean' => true
  51. ];
  52. $options += $defaults;
  53. if (empty($data)) {
  54. return [];
  55. }
  56. $tokens = explode($separator, $data);
  57. if (empty($tokens) || !$options['clean']) {
  58. return $tokens;
  59. }
  60. $tokens = array_map('trim', $tokens);
  61. foreach ($tokens as $key => $token) {
  62. if ($token === '') {
  63. unset($tokens[$key]);
  64. }
  65. }
  66. return $tokens;
  67. }
  68. /**
  69. * Multibyte analogue of preg_match_all() function. Only that this returns the result.
  70. * By default this works properly with UTF8 strings.
  71. *
  72. * Do not forget to use preg_quote() first on strings that could potentially contain
  73. * unescaped characters.
  74. *
  75. * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
  76. *
  77. * Example: /some(.*)pattern/u
  78. *
  79. * @param string $pattern The pattern to use.
  80. * @param string $subject The string to match.
  81. * @param int $flags
  82. * @param int|null $offset
  83. * @return array Result
  84. */
  85. public static function pregMatchAll($pattern, $subject, $flags = PREG_SET_ORDER, $offset = null) {
  86. $pattern = substr($pattern, 0, 1) . '(*UTF8)' . substr($pattern, 1);
  87. preg_match_all($pattern, $subject, $matches, $flags, $offset);
  88. return $matches;
  89. }
  90. /**
  91. * Multibyte analogue of preg_match() function. Only that this returns the result.
  92. * By default this works properly with UTF8 strings.
  93. *
  94. * Do not forget to use preg_quote() first on strings that could potentially contain
  95. * unescaped characters.
  96. *
  97. * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
  98. *
  99. * Example: /some(.*)pattern/u
  100. *
  101. * @param string $pattern The pattern to use.
  102. * @param string $subject The string to match.
  103. * @param int|null $flags
  104. * @param int|null $offset
  105. * @return array Result
  106. */
  107. public static function pregMatch($pattern, $subject, $flags = null, $offset = null) {
  108. $pattern = substr($pattern, 0, 1) . '(*UTF8)' . substr($pattern, 1);
  109. preg_match($pattern, $subject, $matches, $flags, $offset);
  110. return $matches;
  111. }
  112. /**
  113. * Multibyte analogue of str_split() function.
  114. * By default this works properly with UTF8 strings.
  115. *
  116. * @param string $str
  117. * @param int $length
  118. * @return array Result
  119. */
  120. public static function strSplit($str, $length = 1) {
  121. if ($length < 1) {
  122. return false;
  123. }
  124. $result = [];
  125. $c = mb_strlen($str);
  126. for ($i = 0; $i < $c; $i += $length) {
  127. $result[] = mb_substr($str, $i, $length);
  128. }
  129. return $result;
  130. }
  131. /**
  132. * Get the current IP address.
  133. *
  134. * @param bool $safe
  135. * @return string IP address
  136. */
  137. public static function getClientIp($safe = true) {
  138. if (!$safe && env('HTTP_X_FORWARDED_FOR')) {
  139. $ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR'));
  140. } elseif (!$safe && env('HTTP_CLIENT_IP')) {
  141. $ipaddr = env('HTTP_CLIENT_IP');
  142. } else {
  143. $ipaddr = env('REMOTE_ADDR');
  144. }
  145. return trim($ipaddr);
  146. }
  147. /**
  148. * Get the current referrer if available.
  149. *
  150. * @param bool $full (defaults to false and leaves the url untouched)
  151. * @return string referer (local or foreign)
  152. */
  153. public static function getReferer($full = false) {
  154. $ref = env('HTTP_REFERER');
  155. $forwarded = env('HTTP_X_FORWARDED_HOST');
  156. if ($forwarded) {
  157. $ref = $forwarded;
  158. }
  159. if (empty($ref)) {
  160. return $ref;
  161. }
  162. if ($full) {
  163. $ref = Router::url($ref, $full);
  164. }
  165. return $ref;
  166. }
  167. /**
  168. * Remove unnessary stuff + add http:// for external urls
  169. * TODO: protocol to lower!
  170. *
  171. * @param string $url
  172. * @param bool $headerRedirect
  173. * @return string Cleaned Url
  174. */
  175. public static function cleanUrl($url, $headerRedirect = false) {
  176. if ($url === '' || $url === 'http://' || $url === 'http://www' || $url === 'http://www.') {
  177. $url = '';
  178. } else {
  179. $url = static::autoPrefixUrl($url, 'http://');
  180. }
  181. if ($headerRedirect && !empty($url)) {
  182. $headers = static::getHeaderFromUrl($url);
  183. if ($headers !== false) {
  184. $headerString = implode("\n", $headers);
  185. if ((bool)preg_match('#^HTTP/.*\s+[(301)]+\s#i', $headerString)) {
  186. foreach ($headers as $header) {
  187. if (mb_strpos($header, 'Location:') === 0) {
  188. $url = trim(hDec(mb_substr($header, 9))); // rawurldecode/urldecode ?
  189. }
  190. }
  191. }
  192. }
  193. }
  194. $length = mb_strlen($url);
  195. while (!empty($url) && mb_strrpos($url, '/') === $length - 1) {
  196. $url = mb_substr($url, 0, $length - 1);
  197. $length--;
  198. }
  199. return $url;
  200. }
  201. /**
  202. * A more robust wrapper around for file_exists() which easily
  203. * fails to return true for existent remote files.
  204. * Per default it allows http/https images to be looked up via urlExists()
  205. * for a better result.
  206. *
  207. * @param string $file File
  208. * @param string $pattern
  209. * @return bool Success
  210. */
  211. public static function fileExists($file, $pattern = '~^https?://~i') {
  212. if (!preg_match($pattern, $file)) {
  213. return file_exists($file);
  214. }
  215. return static::urlExists($file);
  216. }
  217. /**
  218. * file_exists() does not always work with URLs.
  219. * So if you check on strpos(http) === 0 you can use this
  220. * to check for URLs instead.
  221. *
  222. * @param string $url Absolute URL
  223. * @return bool Success
  224. */
  225. public static function urlExists($url) {
  226. // @codingStandardsIgnoreStart
  227. $headers = @get_headers($url);
  228. // @codingStandardsIgnoreEnd
  229. if ($headers && preg_match('|\b200\b|', $headers[0])) {
  230. return true;
  231. }
  232. return false;
  233. }
  234. /**
  235. * Parse headers from a specific URL content.
  236. *
  237. * @param string $url
  238. * @return mixed array of headers or FALSE on failure
  239. */
  240. public static function getHeaderFromUrl($url) {
  241. // @codingStandardsIgnoreStart
  242. $url = @parse_url($url);
  243. // @codingStandardsIgnoreEnd
  244. if (empty($url)) {
  245. return false;
  246. }
  247. $url = array_map('trim', $url);
  248. $url['port'] = (!isset($url['port'])) ? '' : (':' . (int)$url['port']);
  249. $path = (isset($url['path'])) ? $url['path'] : '';
  250. if (empty($path)) {
  251. $path = '/';
  252. }
  253. $path .= (isset($url['query'])) ? "?$url[query]" : '';
  254. $defaults = [
  255. 'http' => [
  256. 'header' => "Accept: text/html\r\n" .
  257. "Connection: Close\r\n" .
  258. "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64)\r\n",
  259. ]
  260. ];
  261. stream_context_get_default($defaults);
  262. if (isset($url['host']) && $url['host'] !== gethostbyname($url['host'])) {
  263. $url = "$url[scheme]://$url[host]$url[port]$path";
  264. $headers = get_headers($url);
  265. if (is_array($headers)) {
  266. return $headers;
  267. }
  268. }
  269. return false;
  270. }
  271. /**
  272. * Add protocol prefix if necessary (and possible)
  273. *
  274. * @param string $url
  275. * @param string|null $prefix
  276. * @return string
  277. */
  278. public static function autoPrefixUrl($url, $prefix = null) {
  279. if ($prefix === null) {
  280. $prefix = 'http://';
  281. }
  282. $pos = strpos($url, '.');
  283. if ($pos !== false) {
  284. if (strpos(substr($url, 0, $pos), '//') === false) {
  285. $url = $prefix . $url;
  286. }
  287. }
  288. return $url;
  289. }
  290. /**
  291. * Encode strings with base64_encode and also
  292. * replace chars base64 uses that would mess up the url.
  293. *
  294. * Do not use this for querystrings. Those will escape automatically.
  295. * This is only useful for named or passed params.
  296. *
  297. * @deprecated Use query strings instead
  298. * @param string $string Unsafe string
  299. * @return string Encoded string
  300. */
  301. public static function urlEncode($string) {
  302. return str_replace(['/', '='], ['-', '_'], base64_encode($string));
  303. }
  304. /**
  305. * Decode strings with base64_encode and also
  306. * replace back chars base64 uses that would mess up the url.
  307. *
  308. * Do not use this for querystrings. Those will escape automatically.
  309. * This is only useful for named or passed params.
  310. *
  311. * @deprecated Use query strings instead
  312. * @param string $string Safe string
  313. * @return string Decoded string
  314. */
  315. public static function urlDecode($string) {
  316. return base64_decode(str_replace(['-', '_'], ['/', '='], $string));
  317. }
  318. /**
  319. * Returns true only if all values are true.
  320. * //TODO: maybe move to bootstrap?
  321. *
  322. * @param array $array
  323. * @return bool Result
  324. */
  325. public static function logicalAnd($array) {
  326. if (empty($array)) {
  327. return false;
  328. }
  329. foreach ($array as $result) {
  330. if (!$result) {
  331. return false;
  332. }
  333. }
  334. return true;
  335. }
  336. /**
  337. * Returns true if at least one value is true.
  338. * //TODO: maybe move to bootstrap?
  339. *
  340. * @param array $array
  341. * @return bool Result
  342. */
  343. public static function logicalOr($array) {
  344. foreach ($array as $result) {
  345. if ($result) {
  346. return true;
  347. }
  348. }
  349. return false;
  350. }
  351. /**
  352. * On non-transaction db connections it will return a deep array of bools instead of bool.
  353. * So we need to call this method inside the modified saveAll() method to return the expected single bool there, too.
  354. *
  355. * @param array $array
  356. * @return bool
  357. * @deprecated Not sure this is useful for CakePHP 3.0
  358. */
  359. public static function isValidSaveAll($array) {
  360. if (empty($array)) {
  361. return false;
  362. }
  363. $ret = true;
  364. foreach ($array as $key => $val) {
  365. if (is_array($val)) {
  366. $ret = $ret & static::logicalAnd($val);
  367. } else {
  368. $ret = $ret & $val;
  369. }
  370. }
  371. return (bool)$ret;
  372. }
  373. /**
  374. * Convenience function for automatic casting in form methods etc.
  375. * //TODO: maybe move to bootstrap?
  376. *
  377. * @param mixed $value
  378. * @param string $type
  379. * @return mixed Safe value for DB query, or NULL if type was not a valid one
  380. */
  381. public static function typeCast($value, $type) {
  382. switch ($type) {
  383. case 'int':
  384. $value = (int)$value;
  385. break;
  386. case 'float':
  387. $value = (float)$value;
  388. break;
  389. case 'double':
  390. $value = (double)$value;
  391. break;
  392. case 'array':
  393. $value = (array)$value;
  394. break;
  395. case 'bool':
  396. $value = (bool)$value;
  397. break;
  398. case 'string':
  399. $value = (string)$value;
  400. break;
  401. default:
  402. return null;
  403. }
  404. return $value;
  405. }
  406. /**
  407. * Trim recursively
  408. *
  409. * @param mixed $value
  410. * @return array|string
  411. */
  412. public static function trimDeep($value) {
  413. $value = is_array($value) ? array_map('self::trimDeep', $value) : trim($value);
  414. return $value;
  415. }
  416. /**
  417. * Applies h() recursively
  418. *
  419. * @param mixed $value
  420. * @return array|string
  421. */
  422. public static function specialcharsDeep($value) {
  423. $value = is_array($value) ? array_map('self::specialcharsDeep', $value) : htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  424. return $value;
  425. }
  426. /**
  427. * Main deep method
  428. *
  429. * @param callable $function
  430. * @param mixed $value
  431. * @return array|string
  432. */
  433. public static function deep($function, $value) {
  434. $value = is_array($value) ? array_map('self::' . $function, $value) : $function($value);
  435. return $value;
  436. }
  437. /**
  438. * Counts the dimensions of an array. If $all is set to false (which is the default) it will
  439. * only consider the dimension of the first element in the array.
  440. *
  441. * @param array $array Array to count dimensions on
  442. * @param bool $all Set to true to count the dimension considering all elements in array
  443. * @param int $count Start the dimension count at this number
  444. * @return int The number of dimensions in $array
  445. */
  446. public static function countDim($array, $all = false, $count = 0) {
  447. if ($all) {
  448. $depth = [$count];
  449. if (is_array($array) && reset($array) !== false) {
  450. foreach ($array as $value) {
  451. $depth[] = static::countDim($value, true, $count + 1);
  452. }
  453. }
  454. $return = max($depth);
  455. } else {
  456. if (is_array(reset($array))) {
  457. $return = static::countDim(reset($array)) + 1;
  458. } else {
  459. $return = 1;
  460. }
  461. }
  462. return $return;
  463. }
  464. /**
  465. * Expands the values of an array of strings into a deep array.
  466. * Opposite of flattenList().
  467. *
  468. * It needs at least a single separator to be present in all values
  469. * as the key would otherwise be undefined. If data can contain such key-less
  470. * rows, use $undefinedKey to avoid an exception being thrown. But it will
  471. * effectivly collide with other values in that same key then.
  472. *
  473. * So `Some.Deep.Value` becomes `array('Some' => array('Deep' => array('Value')))`.
  474. *
  475. * @param array $data
  476. * @param string $separator
  477. * @param string|null $undefinedKey
  478. * @return array
  479. */
  480. public static function expandList(array $data, $separator = '.', $undefinedKey = null) {
  481. $result = [];
  482. foreach ($data as $value) {
  483. $keys = explode($separator, $value);
  484. $value = array_pop($keys);
  485. $keys = array_reverse($keys);
  486. if (!isset($keys[0])) {
  487. if ($undefinedKey === null) {
  488. throw new RuntimeException('Key-less values are not supported without $undefinedKey.');
  489. }
  490. $keys[0] = $undefinedKey;
  491. }
  492. $child = [$keys[0] => [$value]];
  493. array_shift($keys);
  494. foreach ($keys as $k) {
  495. $child = [
  496. $k => $child
  497. ];
  498. }
  499. $result = Hash::merge($result, $child);
  500. }
  501. return $result;
  502. }
  503. /**
  504. * Flattens a deep array into an array of strings.
  505. * Opposite of expandList().
  506. *
  507. * So `array('Some' => array('Deep' => array('Value')))` becomes `Some.Deep.Value`.
  508. *
  509. * Note that primarily only string should be used.
  510. * However, boolean values are casted to int and thus
  511. * both boolean and integer values also supported.
  512. *
  513. * @param array $data
  514. * @param string $separator
  515. * @return array
  516. */
  517. public static function flattenList(array $data, $separator = '.') {
  518. $result = [];
  519. $stack = [];
  520. $path = null;
  521. reset($data);
  522. while (!empty($data)) {
  523. $key = key($data);
  524. $element = $data[$key];
  525. unset($data[$key]);
  526. if (is_array($element) && !empty($element)) {
  527. if (!empty($data)) {
  528. $stack[] = [$data, $path];
  529. }
  530. $data = $element;
  531. reset($data);
  532. $path .= $key . $separator;
  533. } else {
  534. if (is_bool($element)) {
  535. $element = (int)$element;
  536. }
  537. $result[] = $path . $element;
  538. }
  539. if (empty($data) && !empty($stack)) {
  540. list($data, $path) = array_pop($stack);
  541. reset($data);
  542. }
  543. }
  544. return $result;
  545. }
  546. /**
  547. * Force-flattens an array.
  548. *
  549. * Careful with this method. It can lose information.
  550. * The keys will not be changed, thus possibly overwrite each other.
  551. *
  552. * //TODO: check if it can be replace by Hash::flatten() or Utility::flatten().
  553. *
  554. * @param array $array Array to flatten
  555. * @param bool $preserveKeys
  556. * @return array
  557. */
  558. public static function arrayFlatten($array, $preserveKeys = false) {
  559. if ($preserveKeys) {
  560. return static::_arrayFlatten($array);
  561. }
  562. if (!$array) {
  563. return [];
  564. }
  565. $result = [];
  566. foreach ($array as $key => $value) {
  567. if (is_array($value)) {
  568. $result = array_merge($result, static::arrayFlatten($value));
  569. } else {
  570. $result[$key] = $value;
  571. }
  572. }
  573. return $result;
  574. }
  575. /**
  576. * Force-flattens an array and preserves the keys.
  577. *
  578. * Careful with this method. It can lose information.
  579. * The keys will not be changed, thus possibly overwrite each other.
  580. *
  581. * //TODO: check if it can be replace by Hash::flatten() or Utility::flatten().
  582. *
  583. * @param array $a
  584. * @param array $f
  585. * @return array
  586. */
  587. protected static function _arrayFlatten($a, $f = []) {
  588. if (!$a) {
  589. return [];
  590. }
  591. foreach ($a as $k => $v) {
  592. if (is_array($v)) {
  593. $f = static::_arrayFlatten($v, $f);
  594. } else {
  595. $f[$k] = $v;
  596. }
  597. }
  598. return $f;
  599. }
  600. /**
  601. * Similar to array_shift but on the keys of the array
  602. * like array_shift() only for keys and not values
  603. *
  604. * @param array $array keyValuePairs
  605. * @return string key
  606. */
  607. public static function arrayShiftKeys(&$array) {
  608. foreach ($array as $key => $value) {
  609. unset($array[$key]);
  610. return $key;
  611. }
  612. }
  613. /**
  614. * @var int
  615. */
  616. protected static $_counterStartTime;
  617. /**
  618. * Returns microtime as float value
  619. * (to be subtracted right away)
  620. *
  621. * @param int $precision
  622. * @return float
  623. */
  624. public static function microtime($precision = 8) {
  625. return round(microtime(true), $precision);
  626. }
  627. /**
  628. * @return void
  629. */
  630. public static function startClock() {
  631. static::$_counterStartTime = static::microtime();
  632. }
  633. /**
  634. * @param int $precision
  635. * @param bool $restartClock
  636. * @return float
  637. */
  638. public static function returnElapsedTime($precision = 8, $restartClock = false) {
  639. $startTime = static::$_counterStartTime;
  640. if ($restartClock) {
  641. static::startClock();
  642. }
  643. return static::calcElapsedTime($startTime, static::microtime(), $precision);
  644. }
  645. /**
  646. * Returns microtime as float value
  647. * (to be subtracted right away)
  648. *
  649. * @param int $start
  650. * @param int $end
  651. * @param int $precision
  652. * @return float
  653. */
  654. public static function calcElapsedTime($start, $end, $precision = 8) {
  655. $elapsed = $end - $start;
  656. return round($elapsed, $precision);
  657. }
  658. /**
  659. * Returns pretty JSON
  660. *
  661. * @link https://github.com/ndejong/pretty_json/blob/master/pretty_json.php
  662. * @param string $json The original JSON string
  663. * @param string $indString The string to indent with
  664. * @return string
  665. * @deprecated Now there is a JSON_PRETTY_PRINT option available on json_encode()
  666. */
  667. public static function prettyJson($json, $indString = "\t") {
  668. // Replace any escaped \" marks so we don't get tripped up on quotemarks_counter
  669. $tokens = preg_split('|([\{\}\]\[,])|', str_replace('\"', '~~PRETTY_JSON_QUOTEMARK~~', $json), -1, PREG_SPLIT_DELIM_CAPTURE);
  670. $indent = 0;
  671. $result = '';
  672. $quotemarksCounter = 0;
  673. $nextTokenUsePrefix = true;
  674. foreach ($tokens as $token) {
  675. $quotemarksCounter = $quotemarksCounter + (count(explode('"', $token)) - 1);
  676. if ($token === '') {
  677. continue;
  678. }
  679. if ($nextTokenUsePrefix) {
  680. $prefix = str_repeat($indString, $indent);
  681. } else {
  682. $prefix = null;
  683. }
  684. // Determine if the quote marks are open or closed
  685. if ($quotemarksCounter & 1) {
  686. // odd - thus quotemarks open
  687. $nextTokenUsePrefix = false;
  688. $newLine = null;
  689. } else {
  690. // even - thus quotemarks closed
  691. $nextTokenUsePrefix = true;
  692. $newLine = "\n";
  693. }
  694. if ($token === '{' || $token === '[') {
  695. $indent++;
  696. $result .= $token . $newLine;
  697. } elseif ($token === '}' || $token === ']') {
  698. $indent--;
  699. if ($indent >= 0) {
  700. $prefix = str_repeat($indString, $indent);
  701. }
  702. if ($nextTokenUsePrefix) {
  703. $result .= $newLine . $prefix . $token;
  704. } else {
  705. $result .= $newLine . $token;
  706. }
  707. } elseif ($token === ',') {
  708. $result .= $token . $newLine;
  709. } else {
  710. $result .= $prefix . $token;
  711. }
  712. }
  713. return str_replace('~~PRETTY_JSON_QUOTEMARK~~', '\"', $result);
  714. }
  715. }