PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/fnstep/hydrogen/FNString.php

https://bitbucket.org/valentinknabel/fnstep
PHP | 935 lines | 772 code | 10 blank | 153 comment | 48 complexity | bbaf2beb985382644dae9551f045e4ca MD5 | raw file
  1. <?PHP
  2. //
  3. //!FNStep
  4. //!FNString.php
  5. //
  6. //!Created by Valentin Knabel on 26.02.13
  7. //!Copyright Š 2013 Valentin Knabel. All rights reserved.
  8. //
  9. namespace FNFoundation;
  10. interface FNConstantString {
  11. public static function initWithCString();
  12. }
  13. interface FNConstantNumber {
  14. public static function initWithNumber();
  15. }
  16. class FNString extends FNContainer implements FNCountable {
  17. const CASE_LOWER = MB_CASE_LOWER;
  18. const CASE_UPPER = MB_CASE_UPPER;
  19. const CASE_TITLE = MB_CASE_TITLE;
  20. const UCS_4 = 'UCS-4';const UCS_4BE = 'UCS-4BE';const UCS_4LE = 'UCS-4LE';const UCS_2 = 'UCS-2';const UCS_2BE = 'UCS-2BE';const UCS_2LE = 'UCS-2LE';
  21. const UTF_32 = 'UTF-32';const UTF_32BE = 'UTF-32BE';const UTF_32LE = 'UTF-32LE';const UTF_16 = 'UTF-16';const UTF_16BE = 'UTF-16BE';const UTF_16LE = 'UTF-16LE';
  22. const UTF_7 = 'UTF-7';const UTF7_IMAP = 'UTF7-IMAP';const UTF_8 = 'UTF-8';
  23. const ASCII = 'ASCII';const EUC_JP = 'EUC-JP';const SJIS = 'SJIS';const eucJP_win = 'eucJP-win';const SJIS_win = 'SJIS-win';
  24. const ISO_2022_JP = 'ISO-2022-JP';const ISO_2022_JP_MS = 'ISO-2022-JP-MS';const CP932 = 'CP932';const CP51932 = 'CP51932';
  25. /**PHP5.4
  26. * const MacJapanese = 'MacJapanese';
  27. * const SJIS-DOCOMO = 'SJIS-DOCOMO';
  28. * const SJIS_KDDI = 'SJIS-KDDI';
  29. * const SJIS_SOFTBANK = 'SJIS-SOFTBANK';
  30. * const UTF_8_DOCOMO = 'UTF-8-DOCOMO';
  31. * const UTF_8_Mobile_KDDI_A = 'UTF-8-Mobile#KDDI-A';
  32. * const UTF_8_KDDI = 'UTF-8-KDDI';
  33. * const UTF_8_SOFTBANK = 'UTF-8-SOFTBANK';
  34. * const ISO_2022_JP_KDDI = 'ISO-2022-JP-KDDI';
  35. */
  36. const JIS = 'JIS'; const JIS_ms = 'JIS-ms';const CP50220 = 'CP50220';const CP50220raw = 'CP50220raw';const CP50221 = 'CP50221';const CP50222 = 'CP50222';
  37. const ISO_8859_1 = 'ISO-8859-1';const ISO_8859_2 = 'ISO-8859-2';const ISO_8859_3 = 'ISO-8859-3';const ISO_8859_4 = 'ISO-8859-4';const ISO_8859_5 = 'ISO-8859-5';
  38. const ISO_8859_6 = 'ISO-8859-6';const ISO_8859_7 = 'ISO-8859-7';const ISO_8859_8 = 'ISO-8859-8';const ISO_8859_9 = 'ISO-8859-9';const ISO_8859_10 = 'ISO-8859-10';
  39. const ISO_8859_13 = 'ISO-8859-13';const ISO_8859_14 = 'ISO-8859-14';const ISO_8859_15 = 'ISO-8859-15';const byte2be = 'byte2be';
  40. const byte2le = 'byte2le';const byte4be = 'byte4be';const byte4le = 'byte4le';const BASE64 = 'BASE64';const HTML_ENTITIES = 'HTML-ENTITIES';
  41. const _7bit = '7bit';const _8bit = '8bit';const EUC_CN = 'EUC-CN';const CP936 = 'CP936';
  42. /*const GB18030 = ''; PHP5.4*/
  43. const HZ = 'HZ';const EUC_TW = 'EUC-TW';const CP950 = 'CP950';const BIG_5 = 'BIG-5';const EUC_KR = 'EUC-KR';const UHC = 'UHC';const ISO_2022_KR = 'ISO-2022-KR';
  44. const Windows_1251 = 'Windows-1251';const Windows_1252 = 'Windows-1252';const CP866 = 'CP866';const KOI8_R = 'KOI8-R';
  45. const STANDARD_ENCODING = FNString::UTF_8;
  46. /**
  47. * @static initWithRandom
  48. * @param FNNumber $length = 6
  49. * @param bool $characters = FALSE
  50. * @return FNString
  51. */
  52. static function initWithRandomString(FNNumber $length = NULL, FNString $characters = NULL) {
  53. if($length != NULL) $length = $length->value();
  54. else $length = 6;
  55. if($characters == NULL) $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  56. else $characters = $characters->value();
  57. $string = '';
  58. for($p = 0; $p < $length; $p++) {
  59. $string .= $characters[mt_rand(0, strlen($characters)-1)];
  60. }
  61. return static::initWith($string);
  62. }
  63. static function initWithArray(FNArray $array) {
  64. $value = '';
  65. foreach($array as $string) {
  66. $value .= static::convertValue($string);
  67. }
  68. return static::initWith($value);
  69. }
  70. static function initWithList($arg1, $arg2 = '') {
  71. $value = '';
  72. foreach(func_get_args() as $string) {
  73. $value .= static::convertValue($string);
  74. }
  75. return static::initWith($value);
  76. }
  77. /**
  78. * @method valueWithEncoding
  79. * @param int $encoding
  80. * @return string
  81. */
  82. function valueWithEncoding($encoding = FNString::UTF_8) {
  83. if(function_exists('mb_convert_encoding'))
  84. return mb_convert_encoding($this->value(),$encoding);
  85. else return $this->value;
  86. }
  87. /**
  88. *(non-PHPdoc)
  89. * @see FNScript\FNFoundation.FNContainer::isValueValue($value)
  90. */
  91. static function isValidValue($value) {
  92. if(is_string($value) || is_numeric($value) || $value === NULL || $value instanceof FNContainer)
  93. return true;
  94. return false;
  95. }
  96. /**
  97. *(non-PHPdoc)
  98. * @see FNScript\FNFoundation.FNContainer::convertValue($value)
  99. */
  100. static function convertValue($value) {
  101. if($value instanceof FNContainer)
  102. $value = $value->value();
  103. if(is_string($value) || is_numeric($value)) {
  104. $value =(string)$value;
  105. if(function_exists('mb_detect_encoding') && function_exists('mb_check_encoding') && function_exists('mb_convert_encoding')) {
  106. $encoding = mb_detect_encoding($value);
  107. if(!$encoding) $encoding = FNString::UTF_8;
  108. if(mb_check_encoding($value,$encoding)) $value = mb_convert_encoding($value, FNString::UTF_8,$encoding);
  109. else $value = mb_convert_encoding($value, FNString::UTF_8);
  110. } else {
  111. if(!/*is UTF-8*/preg_match('%^(?:
  112. [\x09\x0A\x0D\x20-\x7E] # ASCII
  113. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  114. | \xE0[\xA0-\xBF][\x80-\xBF] # overlongs
  115. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  116. | \xED[\x80-\x9F][\x80-\xBF] # surrogates
  117. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  118. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  119. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  120. )*$%xs', $value)) {
  121. $value = utf8_encode($value);
  122. }
  123. }
  124. return $value;
  125. } elseif($value === NULL) return '';
  126. else return '';
  127. }
  128. //@MODIFIED
  129. /**
  130. *(non-PHPdoc)
  131. * @see FNScript\FNFoundation.FNContainer::mutableCopy()
  132. */
  133. public function mutableCopy() {
  134. return FNMutableString::initWith($this->value);
  135. }
  136. /**
  137. *(non-PHPdoc)
  138. * @see FNScript\FNFoundation.FNContainer::immutableCopy()
  139. */
  140. public function immutableCopy() {
  141. return FNString::initWith($this->value());
  142. }
  143. /**
  144. * @method count
  145. * @return FNNumber
  146. */
  147. public function count() {
  148. if(function_exists('mb_strlen'))
  149. return FNNumber::initWith(mb_strlen($this->value(),FNString::STANDARD_ENCODING));
  150. else return FNNumber::initWith(strlen(utf8_decode($this->value())));
  151. }
  152. public function __toString() {
  153. return $this->value();
  154. }
  155. ##Substrings
  156. /**
  157. * @method substring
  158. * @param FNNumber $start
  159. * @param FNNumber $length
  160. * @return FNString
  161. */
  162. public function substring(FNNumber $start, FNNumber $length = NULL) {
  163. if($length !== NULL) $length = $length->value();
  164. if(function_exists('mb_substr')) {
  165. return $this->returnObjectWith(mb_substr($this->value(),$start->value(), $length, FNString::STANDARD_ENCODING));
  166. } else return $this->returnObjectWith(substr($this->value(), $start->value(),$length));
  167. }
  168. /**
  169. * @method trimWidth
  170. * @param FNNumber $start
  171. * @param FNNumber $width
  172. * @param FNString $trimmarker
  173. * @return FNString - else false
  174. * @link http://de2.php.net/manual/en/function.mb_strimwdith.php
  175. */
  176. public function trimWidth(FNNumber $start,FNNumber $width,FNString $trimmarker = NULL) {
  177. if(function_exists('mb_strimwidth'))
  178. return $this->returnObjectWith(mb_strimwidth($this->value, $start->value, $width->value,$trimmarker,FNString::STANDARD_ENCODING));
  179. else return false;
  180. }
  181. /**
  182. * @method substringToInsensitve
  183. * @param unknown_type $needle
  184. * @param unknown_type $first
  185. * @return FNString
  186. * @link http://de2.php.net/manual/en/function.mb_stristr.php
  187. * @link http://de2.php.net/manual/en/function.stristr.php
  188. */
  189. function substringToInsensitive(FNString $needle,$first = TRUE) {
  190. if(function_exists('mb_stristr'))
  191. return $this->returnObjectWith(mb_stristr($this->value, $needle->value,$first,FNString::STANDARD_ENCODING));
  192. else return $this->returnObjectWith(stristr($this->value, $needle->value,$first));
  193. }
  194. /**
  195. * @method substringTo
  196. * @param FNString $needle
  197. * @param bool $first
  198. * @return FNString
  199. * @link http://de2.php.net/manual/en/function.mb_strstr.php
  200. * @link http://de2.php.net/manual/en/function.strstr.php
  201. */
  202. function substringTo(FNString $needle,$first = TRUE) {
  203. if(function_exists('mb_strstr'))
  204. return $this->returnObjectWith(mb_strstr($this->value, $needle->value,$first,FNString::STANDARD_ENCODING));
  205. else return $this->returnObjectWith(strstr($this->value, $needle->value,$first));
  206. }
  207. /**
  208. * @method charWithIndex
  209. * @param FNNumber $num
  210. * @return FNString
  211. * @link http://de2.php.net/manual/en/function.chr.php
  212. */
  213. function charWithIndex(FNNumber $num) {
  214. return $this->returnObjectWith(chr($this->value,$num->value));
  215. }
  216. /**
  217. * @method trimLeft
  218. * @param FNString $charlist
  219. * @return FNString
  220. */
  221. function trimLeft(FNString $charlist = NULL) {
  222. if($charlist) $charlist = $charlist->value();
  223. return $this->returnObjectWith(ltrim($this->value(),$charlist));
  224. }
  225. /**
  226. * @method trimRight
  227. * @param FNString $charlist
  228. * @return FNString
  229. */
  230. function trimRight(FNString $charlist) {
  231. if($charlist) $charlist = $charlist->value();
  232. return $this->returnObjectWith(rtrim($this->value,$charlist));
  233. }
  234. /**
  235. * @method substringSinceOccurence
  236. * @param FNString $needle
  237. * @param boolean $before
  238. * @return FNString
  239. */
  240. function substringSinceOccurence(FNString $needle,$before = FALSE) {
  241. if(function_exists('mb_stristr')) {
  242. return $this->returnObjectWith(mb_stristr($this->value(), $needle->value(),$before,FNString::STANDARD_ENCODING));
  243. }
  244. else return $this->returnObjectWith(stristr($this->value,$needle->value,$before));
  245. }
  246. /**
  247. * @method substringSinceChars
  248. * @param FNString $charlist
  249. * @return FNString
  250. */
  251. function substringSinceChars(FNString $charlist) {
  252. return $this->returnObjectWith(strpbrk($this->value,$charlist->value));
  253. }
  254. /**
  255. * @method substringSinceLastChar
  256. * @param FNString $char
  257. * @return FNString
  258. */
  259. function substringSinceLastChar(FNString $char) {
  260. return $this->returnObjectWith(strrchr($this->value,$char->value));
  261. }
  262. /**
  263. * @method appendString
  264. * @param FNString $string
  265. * @return FNString
  266. */
  267. function appendString(FNString $string) {
  268. return $this->returnObjectWith($this->value().$string->value());
  269. }
  270. /**
  271. * @method containsSubstring
  272. * @param FNString $substring
  273. * @return boolean
  274. */
  275. function containsSubstring(FNString $substring) {
  276. return strpos($this->value(), $substring->value()) === FALSE;
  277. }
  278. ##Type-Conversions
  279. /**
  280. * @method bin2hex
  281. * @return FNString
  282. */
  283. function bin2hex() {
  284. return $this->returnObjectWith(bin2hex($this->value));
  285. }
  286. /**
  287. * @method splitRegular
  288. * @param FNString $pattern
  289. * @param FNNumber $limit
  290. * @return FNString
  291. */
  292. public function splitRegular(FNString $pattern,FNNumber $limit = NULL) {
  293. if($limit === NULL) $limit = FNNumber::initWith(-1);
  294. if(function_exists('mb_split'))
  295. return FNArray::initWith(mb_split($pattern->value, $this->value,$limit->value));
  296. else return FNArray::initWith(split($pattern->value, $this->value,$limit->value));
  297. }
  298. /**
  299. * @method explode
  300. * @param FNString $seperator
  301. * @param FNNumber $limit
  302. * @return FNArray
  303. */
  304. function explode(FNString $seperator, FNNumber $limit = null) {
  305. $array = null;
  306. if($limit === NULL) $array = explode($seperator->value,$this->value);
  307. else $array = explode($seperator->value,$this->value,$limit->value);
  308. foreach($array as $key => $value) {
  309. $array[$key] = FNString::initWith($value);
  310. }
  311. return FNArray::initWith($array);
  312. }
  313. /**
  314. * @method parse
  315. * @param FNArray $data
  316. * @return void
  317. */
  318. function parse(FNArray &$data) {
  319. $arr = NULL;
  320. parse_str($this->value,$arr);
  321. $data = FNArray::initWith($arr);
  322. } //@MODIFIED - $arr instead of &$arr(Deprecated)
  323. /**
  324. * @method csvArray
  325. * @param FNString $seperator
  326. * @param FNString $enclosure
  327. * @param FNString $escape
  328. * @return FNArray
  329. */
  330. function csvArray(FNString $seperator = NULL,FNString $enclosure = NULL, FNString $escape = NULL) {
  331. if($seperator === NULL) $seperator = FNString::initWith(',');
  332. if($enclosure === NULL) $enclosure = FNString::initWith('"');
  333. if($escape === NULL) $escape = FNString::initWith('\\');
  334. return FNArray::initWith(str_getcsv($this->value,$seperator->value,$enclosure,$escape));
  335. }
  336. /**
  337. * @method split
  338. * @return FNArray
  339. */
  340. function split() {
  341. return FNArray::initWith(str_split($this->value));
  342. }
  343. ##Encoding-Conversions
  344. /**
  345. * urlEncode
  346. * @author Valentin Knabel
  347. * @since 27.05.12
  348. * @revision 4518745504
  349. */
  350. function urlEncode( ) {
  351. return $this-> returnObjectWith(urlencode($this->value));
  352. }
  353. /**
  354. * urlDecode
  355. * @author Valentin Knabel
  356. * @since 27.05.12
  357. * @revision 4525668800
  358. */
  359. function urlDecode( ) {
  360. return $this-> returnObjectWith(urldecode($this->value));
  361. }
  362. /**
  363. * @method convertKana
  364. * @param FNString $option
  365. * @return FNString - else false
  366. * @link http://cn.php.net/manual/en/function.mb-convert-kana.php
  367. */
  368. public function convertKana(FNString $option = NULL) {
  369. if($option) $option = $option->value;
  370. if(function_exists('mb_convert_kana'))
  371. return $this->returnObjectWith(mb_convert_kana($this->value,$option->value,FNString::STANDARD_ENCODING));
  372. else false;
  373. }
  374. /**
  375. * @method convertCyrillic
  376. * @deprecated
  377. * @param FNString $to
  378. * @return FNString
  379. */
  380. function convertCyrillic(FNString $to) {
  381. return $this->returnObjectWith(convert_cyr_string($this->value,$this->encoding,$to));
  382. }
  383. /**
  384. * @method convertUudecode
  385. * @deprecated unsafe
  386. * @return FNString
  387. */
  388. function convertUudecode() {
  389. return $this->returnObjectWith(convert_uudecode($this->value));
  390. }
  391. /**
  392. * @method convertUuencode
  393. * @deprecated unsafe
  394. * @return FNString
  395. */
  396. function convertUuencode() {
  397. return $this->returnObjectWith(convert_uuencode($this->value));
  398. }
  399. ##Case-Conversions
  400. /**
  401. * @method convertCase
  402. * @param int $mode
  403. * @return FNString
  404. * @link http://cn.php.net/manual/en/function.mb-convert-case.php
  405. * @link http://cn.php.net/manual/en/function.strtoupper.php
  406. * @link http://cn.php.net/manual/en/function.strtolower.php
  407. * @link http://cn.php.net/manual/en/function.ucwords.php
  408. */
  409. public function convertCase($mode) {
  410. if(function_exists('mb_convert_case'))
  411. return $this->returnObjectWith(mb_convert_case($this->value, $mode->value,FNString::STANDARD_ENCODING));
  412. elseif($mode == FNString::CASE_UPPER) return $this->returnObjectWith(strtoupper($this->value));
  413. elseif($mode == FNString::CASE_LOWER) return $this->returnObjectWith(strtolower($this->value));
  414. elseif($mode == FNString::CASE_TITLE) return $this->returnObjectWith(ucwords($this->value));
  415. }
  416. /**
  417. * @method lowerCase
  418. * @return FNString
  419. */
  420. function lowerCase() {
  421. if(function_exists('mb_strtolower'))
  422. return $this->returnObjectWith(mb_strtolower($this->value,FNString::STANDARD_ENCODING));
  423. else return $this->returnObjectWith(strtolower($this->value));
  424. }
  425. /**
  426. * @method upperCase
  427. * @return FNString
  428. */
  429. function upperCase() {
  430. if(function_exists('mb_strtoupper'))
  431. return $this->returnObjectWith(mb_strtoupper($this->value,FNString::STANDARD_ENCODING));
  432. else return $this->returnObjectWith(strtoupper($this->value));
  433. }
  434. /**
  435. * @method firstCharacterToLowerCase
  436. * @return FNString
  437. */
  438. function firstCharacterToLowerCase() {
  439. return $this->returnObjectWith(lcfirst($this->value,$data->value));
  440. }
  441. ##RegExp
  442. /**
  443. * @method eregMatch
  444. * @param FNString $pattern
  445. * @param FNString $option
  446. * @return FNString
  447. */
  448. public function eregMatch(FNString $pattern,FNString $option = NULL) {
  449. if($option === NULL) $option = FNString::initWith('msr');
  450. return $this->returnObjectWith(mb_ereg_match($pattern->value,$option->value));
  451. }
  452. /**
  453. * @method eregReplace
  454. * @param FNString $pattern
  455. * @param FNString $replacement
  456. * @return FNString
  457. */
  458. public function eregReplace(FNString $pattern,FNString $replacement) {
  459. return $this->returnObjectWith(mb_ereg_replace($pattern->value, $replacement->value, $this->value));
  460. }
  461. /**
  462. * @method ereg
  463. * @param FNString $pattern
  464. * @return FNString
  465. */
  466. public function ereg(FNString $pattern) {
  467. return $this->returnObjectWith(mb_ereg($pattern->value, $this->value));
  468. }
  469. /**
  470. * @method eregIntensiveReplace
  471. * @param FNString $pattern
  472. * @param FNString $replacement
  473. * @param FNString $option
  474. * @return FNString
  475. */
  476. public function eregInsensitiveReplace(FNString $pattern,FNString $replacement,FNString $option = NULL) {
  477. if($option === NULL) $option = FNString::initWith('msri');
  478. return $this->returnObjectWith(mb_eregi_replace($pattern->value, $replace->value, $this->value,$option->value));
  479. }
  480. /**
  481. * @method eregIntensive
  482. * @param FNString $pattern
  483. * @return FNString
  484. */
  485. public function eregInsensitive(FNString $pattern) {
  486. return $this->returnObjectWith(mb_eregi($pattern->value, $this->value));
  487. }
  488. ##Security
  489. /**
  490. * @method crc32
  491. * @param FNString $data
  492. * @return FNNumber
  493. */
  494. function crc32() {
  495. return FNNumber::initWith(crc32($this->value));
  496. }
  497. /**
  498. * @method crypt
  499. * @param FNString $salt
  500. * @return FNString
  501. */
  502. function crypt(FNString $salt = null) {
  503. if($salt) $salt = $salt->value();
  504. return $this->returnObjectWith(convert_uudecode($this->value,$salt));
  505. }
  506. /**
  507. * @method levenshtein
  508. * @param FNString $data
  509. * @return FNNumber
  510. */
  511. function levenshtein(FNString $data) {
  512. return FNNumber::initWith(levenshtein($this->value,$data->value));
  513. }
  514. /**
  515. * @method md5
  516. * @param boolean $data
  517. * @return FNString
  518. */
  519. function md5( $raw = false) {
  520. return $this->returnObjectWith(md5($this->value,$raw));
  521. }
  522. /**
  523. * @method ord
  524. * @return FNNumber
  525. */
  526. function ord() {
  527. $char = $this->charWithIndex(FNNumber::zero());
  528. $i = 0;
  529. $number = '';
  530. while(isset($char{$i})) {
  531. $number.= ord($char{$i});
  532. ++$i;
  533. }
  534. return $number;
  535. return FNNumber::initWith($number);
  536. }
  537. /**
  538. * @method sha1Hash
  539. * @param boolean $data
  540. * @return FNString
  541. */
  542. function sha1Hash($data = false) {
  543. return $this->returnObjectWith(sha1($this->value,$data));
  544. }
  545. /**
  546. * @method similarity
  547. * @param FNString $data
  548. * @return FNNumber
  549. */
  550. function similarity(FNString $data) {
  551. return FNNumber::initWith(similar_text($this->value,$data->value));
  552. }
  553. /**
  554. * @method rot13
  555. * @return FNString
  556. */
  557. function rot13() {
  558. return $this->returnObjectWith(str_rot13($this->value));
  559. }
  560. ##manipulation
  561. /**
  562. * @method addSlashes
  563. * @param FNString $charlist
  564. * @return FNString
  565. */
  566. function addSlashes(FNString $charlist) {
  567. return $this->returnObjectWith(addSlashes($this->value,$charlist->value));
  568. }
  569. /**
  570. * @method addCSlashes
  571. * @param FNString $charlist
  572. * @return FNString
  573. */
  574. function addCSlashes(FNString $charlist) {
  575. return $this->returnObjectWith(addCSlashes($this->value,$charlist->value));
  576. }
  577. /**
  578. * @method reverseHebrew
  579. * @param FNNumber $maxChars
  580. * @return FNString
  581. */
  582. function reverseHebrew(FNNumber $maxChars) {
  583. return $this->returnObjectWith(hebrev($this->value,$maxChars->value));
  584. }
  585. /**
  586. * @method reverseHebrewBr
  587. * @param FNNumber $maxChars
  588. * @return FNString
  589. */
  590. function reverseHebrewBr(FNNumber $maxChars) {
  591. return $this->returnObjectWith(hebrevc($this->value,$maxChars->value));
  592. }
  593. /**
  594. * @method decodeHTMLEntities
  595. * @param FNNumber $flags
  596. * @return FNString
  597. */
  598. function decodeHTMLEntities(FNNumber $flags = NULL) {
  599. if($flags) $flags = $flags->value();
  600. return $this->returnObjectWith(html_entity_decode($this->value,$flags,FNString::STANDARD_ENCODING));
  601. }
  602. /**
  603. * @method encodeHTMLEntities
  604. * @param FNString $data
  605. * @param boolean $doubleEncode
  606. * @return FNString
  607. */
  608. function encodeHTMLEntities(FNString $data = NULL, $doubleEncode = TRUE) {
  609. if($data === NULL) $data = FNString::initWith(ENT_COMPFN | ENT_HTML401);
  610. return $this->returnObjectWith(htmlentities($this->value,$data->value,FNString::STANDARD_ENCODING,$doubleEncode));
  611. }
  612. /**
  613. * @method decodeSpecialHTMLChars
  614. * @param FNNumber $flags
  615. * @return FNString
  616. */
  617. function decodeSpecialHTMLChars(FNNumber $flags = NULL) {
  618. if($flags === NULL) $flags = FNNumber::initWith(ENT_COMPFN);
  619. return $this->returnObjectWith(htmlspecialchars_decode($this->value,$flags->value));
  620. }
  621. /**
  622. * @method encodeSpecialHTMLChars
  623. * @param FNNumber $flags
  624. * @param boolean $doubleEncode
  625. * @return FNString
  626. */
  627. function encodeSpecialHTMLChars(FNNumber $flags = NULL,$doubleEncode = true) {
  628. if($flags === NULL) $flags = FNNumber::initWith(ENT_COMPFN | ENT_HTML401);
  629. return $this->returnObjectWith(convert_uudecode($this->value,$flags->value,FNString::STANDARD_ENCODING,$doubleEncode));
  630. }
  631. /**
  632. * @method newLine2Br
  633. * @param boolean $data
  634. * @return FNString
  635. */
  636. function newLine2Br( $xhtml = TRUE) {
  637. return $this->returnObjectWith(nl2br($this->value,$xhtml));
  638. }
  639. /**
  640. * @method quoteMetaChars
  641. * @return FNString
  642. */
  643. function quoteMetaChars() {
  644. return $this->returnObjectWith(quotemeta($this->value));
  645. }
  646. /**
  647. * @method shuffle
  648. * @return FNString
  649. */
  650. function shuffle() {
  651. return $this->returnObjectWith(str_shuffle($this->value));
  652. }
  653. /**
  654. * @method stripTags
  655. * @param FNString $allowedTags
  656. * @return FNString
  657. */
  658. function stripTags(FNString $allowedTags = NULL) {
  659. if($allowedTags) $allowedTags = $allowedTags->value();
  660. return $this->returnObjectWith(strip_tags($this->value,$allowedTags));
  661. }
  662. /**
  663. * @method stripCSlashes
  664. * @return FNString
  665. */
  666. function stripCSlashes() {
  667. return $this->returnObjectWith(stripcslashes($this->value));
  668. }
  669. /**
  670. * @method stripSlashes
  671. * @return FNString
  672. */
  673. function stripSlashes() {
  674. return $this->returnObjectWith(stripslashes($this->value));
  675. }
  676. /**
  677. * @method reverse
  678. * @return FNString
  679. */
  680. function reverse() {
  681. return $this->returnObjectWith(strrev($this->value));
  682. }
  683. /**
  684. * @method replaceSubstrings
  685. * @param FNString $search
  686. * @param FNString $to
  687. * @return FNString
  688. */
  689. function replaceSubstrings(FNString $search, FNString $to) {
  690. return $this->returnObjectWith(strtr($this->value,$search->value,$to->value));
  691. }
  692. /**
  693. * @method trim
  694. * @param FNString $charlist
  695. * @return FNString
  696. */
  697. function trim(FNString $charlist = null) {
  698. if($charlist) $charlist = $charlist->value();
  699. return $this->returnObjectWith(trim($this->value,$charlist));
  700. }
  701. /**
  702. * @method wrapWords
  703. * @param FNNumber $data
  704. * @param FNNumber $width
  705. * @param FNString $break
  706. * @param boolean $cut
  707. * @return FNString
  708. */
  709. function wrapWords(FNNumber $data,FNNumber $width = NULL,FNString $break = NULL,$cut = false) {
  710. if($width === NULL) $width = FNNumber::initWith(75);
  711. if($break === NULL) $break = FNString::initWith('\n');
  712. return $this->returnObjectWith(wordwrap($this->value,$data->value,$width->value,$break->value,$cut));
  713. }
  714. /**
  715. * @method replaceSubstring
  716. * @param FNString $replace
  717. * @param FNNumber $start
  718. * @param FNNumber $length
  719. * @return FNString
  720. */
  721. function replaceSubstring(FNString $replace, FNNumber $start, FNNumber $length = null) {
  722. if($length) $length = $length->value();
  723. return $this->returnObjectWith(substr_replace($this->value(),$replace->value(),$start->value(),$length));
  724. }
  725. /**
  726. * @method intensiveRelace
  727. * @param FNString $data
  728. * @param FNString $replace
  729. * @param FNNumber $count
  730. * @return FNString
  731. */
  732. function insisitiveReplace(FNString $data,FNString $replace, &$count = null) {
  733. $return = $this->returnObjectWith(str_ireplace($data->value,$replace->value,$this->value,$count)); //@MODIFIED $count instead of &$count(Deprecated)
  734. $count = FNNumber::initWith($count);
  735. return $return;
  736. }
  737. /**
  738. * @method pad
  739. * @param FNNumber $data
  740. * @param FNString $pad
  741. * @param FNNumber $padType
  742. */
  743. function pad(FNNumber $data, FNString $pad = NULL, FNNumber $padType = NULL) {
  744. if($pad === NULL) $pad = FNString::initWith(' ');
  745. if($padType === NULL) $padType = FNNumber::initWith(STR_PAD_RIGHT);
  746. return $this->returnObjectWith(str_pad($this->value,$data->value, $pad->value,$padType->value));
  747. }
  748. /**
  749. * @method repeat
  750. * @param FNNumber $times
  751. * @return FNString
  752. */
  753. function repeat(FNNumber $times) {
  754. return $this->returnObjectWith(str_repeat($this->value,$times->value));
  755. }
  756. /**
  757. * @method replace
  758. * @param FNString $search
  759. * @param FNString $replace
  760. * @param FNNumber &$count
  761. * @return FNString
  762. */
  763. function replace(FNString $search,FNString $replace, &$count = null) {
  764. $return = $this->returnObjectWith(str_replace($search->value(),$replace->value(),$this->value(),$count));//@MODIFIED $count instead of &$count(Deprecated)
  765. $count = FNNumber::initWith($count);
  766. return $return;
  767. }
  768. ##count/position
  769. /**
  770. * @method countWords
  771. * @param FNNumber $data
  772. * @param FNString $charlist
  773. * @return FNString
  774. */
  775. function countWords(FNNumber $option = NULL,FNString $charlist = NULL) {
  776. if(!$option) $option = FNNumber::zero();
  777. if($charlist === NULL) $charlist = FNString::initWith('');
  778. return $this->returnObjectWith(str_word_count($this->value,$data->value,$charlist->value));
  779. }
  780. /**
  781. * @method positionOfMissingChar
  782. * @param FNString $char
  783. * @param FNNumber $start
  784. * @param FNNumber $length
  785. * @return FNNumber
  786. */
  787. function positionOfMissingChar(FNString $char, FNNumber $start = NULL,FNNumber $length = null) {
  788. if($start === NULL) $start = FNNumber::initWith(0);
  789. return $this->returnObjectWith(strcspn($this->value,$char->value,$start->value,$length->value));
  790. }
  791. /**
  792. * @method positionOf
  793. * @param FNString $data
  794. * @return FNNumber
  795. */
  796. function positionOf(FNString $data) {
  797. return FNNumber::initWith(strpos($this->value(),$data->value()));
  798. }
  799. /**
  800. * @method insensitivePositionOf
  801. * @param FNString $data
  802. * @param FNNumber $offset
  803. * @return FNNumber
  804. */
  805. function insensitivePositionOf(FNString $data,FNNumber $offset = NULL) {
  806. if($offset === NULL) $offset = FNNumber::initWith(0);
  807. return FNNumber::initWith(stripos($this->value,$data->value,$offset->value));
  808. }
  809. /**
  810. * @method positionOfLastInsesitiveOccurence
  811. * @param FNString $data
  812. * @param FNNumber $offset
  813. * @return FNNumber
  814. */
  815. function positionOfLastInsensitiveOccurence(FNString $data, FNNumber $offset = NULL) {
  816. if($offset === NULL) $offset = FNNumber::initWith(0);
  817. return $this->returnObjectWith(strripos($this->value,$data->value,$offset->value));
  818. }
  819. /**
  820. * @method positionOfLastOccurence
  821. * @param FNString $data
  822. * @param FNNumber $offset
  823. * @return FNNumber
  824. */
  825. function positionOfLastOccurence(FNString $data, FNNumber $offset = NULL) {
  826. if($offset === NULL) $offset = FNNumber::initWith(0);
  827. return $this->returnObjectWith(strrpos($this->value,$data->value,$offset->value));
  828. }
  829. /**
  830. * @method lengthOfChars
  831. * @param FNString $data
  832. * @param FNNumber $start
  833. * @param FNNumber $length
  834. * @return FNNumber
  835. */
  836. function lengthOfSubstringOfChars(FNString $data, FNNumber $start = NULL, FNNumber $length = null) {
  837. if($start === NULL) $start = FNNumber::initWith(0);
  838. return $this->returnObjectWith(strspn($this->value,$data->value,$start->value,$length->value));
  839. }
  840. /**
  841. * @method countSubstring
  842. * @param FNString $needle
  843. * @return FNNumber
  844. */
  845. function countSubstring(FNString $needle) {
  846. if(function_exists('mb_substr_count'))
  847. return FNNumber::initWith(mb_substr_count($this->value, $needle->value));
  848. else return FNNumber::initWith(substr_count($this->value, $needle->value));
  849. }
  850. /**
  851. * @method positionOfLastInsensitive
  852. * @param FNString $data
  853. * @param FNNumber $offset
  854. * @return FNNumber
  855. */
  856. function positionOfLastInsensitive(FNString $data, FNNumber $offset = NULL) {
  857. if($offset === NULL) $offset = FNNumber::initWith(0);
  858. if(function_exists('mb_stripos'))
  859. return $this->returnObjectWith(mb_stripos($this->value,$data->value,$offset->value,FNString::STANDARD_ENCODING));
  860. else return $this->returnObjectWith(stripos($this->value,$data->value,$offset->value));
  861. }
  862. ##Others
  863. /**
  864. * @method stringWidth
  865. * @return FNNumber
  866. */
  867. function stringWidth() {
  868. if(function_exists('mb_strwidth'))
  869. return FNNumber::initWith(mb_strwidth($this->value,FNString::STANDARD_ENCODING));
  870. else return FNNumber::zero();
  871. }
  872. /**
  873. * @method metaphone
  874. * @param FNNumber $data
  875. * @return FNString
  876. */
  877. function metaphone(FNNumber $data = NULL) {
  878. if($data === NULL) $data = FNNumber::initWith(0);
  879. return $this->returnObjectWith(metaphone($this->value,$data->value));
  880. }
  881. /**
  882. * @method soundex
  883. * @return FNString
  884. */
  885. function soundex() {
  886. return $this->returnObjectWith(soundex($this->value));
  887. }
  888. function format(FNContainer $container = NULL /*infinite arguments*/) {
  889. $array = array($this->value());
  890. foreach(func_get_args() as $arg) {
  891. if(function_exists(array($arg,'value')))
  892. $array[] = $arg->value();
  893. else $array[] = FNString::initWith('');
  894. }
  895. return $this->returnObjectWith(call_user_func_array('sprintf', func_get_args()));
  896. }
  897. function formatArray(FNArray $containers) {
  898. $array = array($this->value());
  899. foreach($containers as $arg) {
  900. if(function_exists(array($arg,'value')))
  901. $array[] = $arg->value();
  902. else $array[] = FNString::initWith('');
  903. }
  904. return $this->returnObjectWith(call_user_func_array('sprintf', func_get_args()));
  905. }
  906. }
  907. class FNMutableString extends FNString implements FNMutable {}//@MODIFIED
  908. ?>