1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-09-01 09:03:03 +02:00

180 Commits

Author SHA1 Message Date
Daniel St. Jules
4749c205db 1.10.0 2015-07-22 17:54:12 -07:00
Daniel St. Jules
fe3368bd8b Add trimLeft, trimRight, support unicode whitespace 2015-07-22 17:41:19 -07:00
Daniel St. Jules
7763df3c3b Add public delimit() function 2015-07-01 19:25:35 -07:00
Daniel St. Jules
397d2ca2f8 Style fixes 2015-06-29 15:33:00 -07:00
Daniel St. Jules
2c295fd15d Merge branch 'indexOf' 2015-06-29 14:56:25 -07:00
Tadeu Bento
9a5601efef Added indexOf and indexOfLast 2015-06-29 14:45:32 -07:00
Daniel St. Jules
e42f165471 Merge branch 'trim' 2015-06-29 13:03:13 -07:00
Heudon
1a2e771ff8 Added $charList and $type to trim function
Adding the option to trim the string with others characters, not only with the default characters provided by php. Also added the option to define the function which will be used to trim the string, if it's normal trim, left trim or right trim
2015-06-29 13:02:09 -07:00
Daniel St. Jules
f9eab1b657 Merge pull request #91 from jcroll/htmlEncode
Added htmlEncode and htmlDecode
2015-06-29 12:53:04 -07:00
Jarrett Croll
f9729788c4 html encode and decode 2015-06-16 16:20:41 -04:00
Daniel St. Jules
f1009d0267 Add SubStringy to readme 2015-06-08 00:56:20 -07:00
Daniel St. Jules
9d1073f4f5 Merge pull request #84 from emnsen/patch-1
Added Turkish Character
2015-04-16 10:10:55 -07:00
Emin Şen
c2a554253c Added Turkish Character
Added Turkish Character ; Ç
2015-03-11 16:21:23 +02:00
Daniel St. Jules
3cf18e9e42 1.9.0 2015-02-09 22:19:18 -08:00
Daniel St. Jules
04b12c9500 Document hasLowerCase and hasUpperCase 2015-02-09 22:10:46 -08:00
Daniel St. Jules
b2386b6c33 Sticking to the ruler where possible 2015-02-09 21:38:42 -08:00
Daniel St. Jules
1172a75d1e Improve coverage of charsArray() 2015-02-09 21:27:21 -08:00
Daniel St. Jules
4ebf9aee3d Merge pull request #79 from vlakoff/tweak
Just a little bit of tweaking
2015-02-03 02:21:24 -08:00
vlakoff
61b986a3e6 Code tweaking
Prettier, one less instanciation.
2015-02-03 11:13:58 +01:00
vlakoff
2bd4be60bb Uniformization in docblocks 2015-02-03 11:10:27 +01:00
Daniel St. Jules
0aa476d44c Merge pull request #68 from vlakoff/toAscii
[Proposal] Add $replaceUnsupported and $fallback parameters to toAscii()
2015-02-02 23:42:47 -08:00
Daniel St. Jules
f63646dcba Merge pull request #78 from vlakoff/readme
Clean up some commas in README
2015-02-02 23:41:43 -08:00
vlakoff
97e607dc5d Add $removeUnsupported parameter to toAscii() 2015-02-03 07:12:02 +01:00
vlakoff
a9a4ce461f Clean up some commas in README 2015-02-03 06:29:04 +01:00
Daniel St. Jules
9a0f0e16cb Merge pull request #77 from takobell/master
Add Stringy->hasUpperCase, Stringy->hasLowerCase along with StaticString counterparts & tests.
2015-02-01 22:09:48 -08:00
Brad Bell
e9cd229808 Add StaticStringy::hasUpperCase, StaticStringy::hasLowerCase, Stringy->hasUpperCase and Stringy->hasLowerCase along with tests. 2015-01-23 15:52:58 -08:00
Daniel St. Jules
4974407729 charsArray formatting 2015-01-18 20:14:57 -08:00
Daniel St. Jules
8a36b8d3f0 Merge pull request #74 from anhskohbo/master
Support Full Unicode For Vietnamese.
2015-01-18 20:11:40 -08:00
Daniel St. Jules
66c2a98e83 Merge pull request #75 from vlakoff/tweak
Minor improvements
2015-01-16 18:50:54 -08:00
vlakoff
36c3fd05d2 Use Stringy's trim() method in applyDelimiter()
This trim() method may be more powerful in the future.
2015-01-17 01:53:01 +01:00
vlakoff
f3bd5b6c62 Fix method name
applyDelimeter() --> applyDelimiter()

Thankfully, it is a protected method.
2015-01-17 01:52:55 +01:00
vlakoff
dd34bc6f85 Minor code adjustment 2015-01-17 01:52:46 +01:00
anhskohbo
f490aa2894 Support Full Unicode For Vietnamese.
Signed-off-by: anhskohbo <anhskohbo@gmail.com>
2015-01-14 03:43:59 +07:00
Daniel St. Jules
40974608d4 Merge pull request #70 from vlakoff/toAscii-2
Separate toAscii()'s charsArray to allow customization
2015-01-13 12:25:16 -08:00
vlakoff
8676bd9d9b Add cache in charsArray()
This is static but confined to this function block,
so this doesn't introduce any side effect.

The performance gain is really worth it.
2015-01-12 19:59:37 +01:00
Daniel St. Jules
f4327a2583 Merge pull request #73 from lucasmichot/missing-space
Style fix
2015-01-11 17:39:30 -08:00
Lucas Michot
571dc93d1e Add missing space 2015-01-12 02:38:52 +01:00
vlakoff
62f6fd7b3a Separate toAscii()'s charsArray to allow customization 2015-01-10 22:39:36 +01:00
Daniel St. Jules
8b834482df Merge pull request #72 from vlakoff/spaces
Add remaining Unicode spaces
2015-01-09 19:58:36 -08:00
vlakoff
08a72a72dd Add remaining Unicode spaces
* spaces U+2000 to U+200A (there already was thin space U+2009)
* medium mathematical space (U+205F)
2015-01-09 19:14:16 +01:00
Daniel St. Jules
6a7b0391b2 1.8.1 2015-01-08 07:21:43 -08:00
Daniel St. Jules
d6fa00bb1c Conditionally load Stringy\create 2015-01-07 09:09:07 -08:00
Daniel St. Jules
d1452263bd Merge pull request #59 from usernam3/master
Typecast comparison in startsWith/endsWith
2015-01-06 21:42:16 -08:00
Stanislav Dobrovolskiy
9da55d5d4f type cast in startsWith && endsWith, test cases for this behavior 2015-01-07 06:06:53 +02:00
Daniel St. Jules
b515598227 Merge pull request #67 from vlakoff/regex
Remove some useless (and erroneous) regex capturing groups
2015-01-06 16:42:30 -08:00
Daniel St. Jules
28b65615d7 Merge pull request #66 from vlakoff/nbsp
Support for various Unicode spaces in toAscii()
2015-01-06 16:38:55 -08:00
vlakoff
7b88b4f3da Support for various Unicode spaces in toAscii()
* no-break space (U+00A0)
* thin space (U+2009)
* narrow no-break space (U+202F)
* ideographic space (U+3000)
2015-01-07 01:28:49 +01:00
vlakoff
70e5b9c38b Remove some useless (and erroneous) regex capturing groups 2015-01-07 01:24:40 +01:00
vlakoff
e0d50f0b8f Unit tests for no-break space in collapseWhitespace() and isBlank() 2015-01-07 01:10:30 +01:00
Daniel St. Jules
c5365f256f Merge pull request #64 from vlakoff/tests
Add missing tests in StringyTest
2015-01-06 13:42:20 -08:00
Daniel St. Jules
401af084c7 Merge pull request #63 from vlakoff/docblock
Proper Stringy\create() description
2015-01-05 16:37:27 -08:00
vlakoff
773477cdd8 Add missing tests in StringyTest
For consistency with StaticStringyTest.
2015-01-06 00:39:31 +01:00
vlakoff
26444e4429 Proper Stringy\create() description 2015-01-06 00:10:39 +01:00
Daniel St. Jules
f3d32c79b0 Merge pull request #62 from vlakoff/static
Replace self::create() with static::create()
2015-01-05 14:06:06 -08:00
vlakoff
98c1013623 Replace self::create() with static::create()
In case someone wants to extend create().
2015-01-05 23:02:06 +01:00
Daniel St. Jules
91d8da4741 Cleanup toAscii array alignment 2015-01-05 13:55:08 -08:00
Daniel St. Jules
49cdd5b1e6 Merge pull request #61 from vlakoff/toAscii
Add « ä Ä Ö Ü » in toAscii()
2015-01-05 13:38:30 -08:00
vlakoff
2fe42b46a1 Add « ä Ä Ö Ü » in toAscii()
To complete diareses on vowels.
2015-01-05 22:20:22 +01:00
Daniel St. Jules
9baa545f23 Merge pull request #60 from vlakoff/optimize
Optimize chars() by not repeatedly call length()
2015-01-05 12:08:58 -05:00
vlakoff
640fc5b26b Optimize chars() by not repeatedly call length() 2015-01-05 13:49:48 +01:00
Daniel St. Jules
d661047fb3 1.8.0 2015-01-03 22:54:13 -08:00
Daniel St. Jules
26fe399396 Merge pull request #58 from GrahamCampbell/patch-1
Remove duplicate dependencies from autoload-dev
2015-01-03 19:26:38 -05:00
Daniel St. Jules
31c4ba145f Merge pull request #57 from vlakoff/dependency
Add ext-mbstring to composer.json
2015-01-03 19:25:41 -05:00
Graham Campbell
5a1be9bf20 autoload-dev behave the same as require-dev 2015-01-03 22:55:35 +00:00
vlakoff
28c3db01c5 Add ext-mbstring to composer.json 2015-01-03 23:20:21 +01:00
Daniel St. Jules
27ef5a8914 Add Stringy\create function for PHP 5.6 2015-01-01 21:40:48 -08:00
Daniel St. Jules
350d1e6f8e Merge pull request #56 from GrahamCampbell/patch-1
Tweaked the test config
2014-10-26 07:47:54 -07:00
Graham Campbell
ce58c93fc2 Tweaked the test config 2014-10-26 12:44:18 +00:00
Daniel St. Jules
d60422d197 1.7.0 2014-10-14 23:32:13 -07:00
Daniel St. Jules
3cc12c2ee7 Added containsAll and containsAny to readme 2014-10-14 23:25:43 -07:00
Daniel St. Jules
8e16ad472d Add docblocks for str and encoding 2014-10-14 23:14:02 -07:00
Daniel St. Jules
3f926c654a Merge pull request #49 from lucasmichot/factorize-substr
Factorize substr
2014-09-29 08:02:06 -07:00
Daniel St. Jules
f8fc711ae1 Merge pull request #51 from lucasmichot/remove-useless-variable
Remove useless variable
2014-09-29 07:58:45 -07:00
Daniel St. Jules
628c467a67 Merge pull request #48 from lucasmichot/factorize-tidy
Factorize tidy
2014-09-29 07:58:29 -07:00
Daniel St. Jules
24aa843639 Merge pull request #47 from lucasmichot/factorize-humanize
Factorize humanize
2014-09-29 07:43:43 -07:00
Lucas Michot
bb6ea99c52 Remove useless variable 2014-09-29 14:24:02 +02:00
Lucas Michot
d1df244678 Use $this 2014-09-29 14:05:35 +02:00
Lucas Michot
9a929d1860 Simplify substr 2014-09-29 13:48:30 +02:00
Lucas Michot
583789b9e3 Simplify tidy 2014-09-29 13:13:55 +02:00
Lucas Michot
f00e3d5d80 Simplify humanize 2014-09-29 13:08:57 +02:00
Daniel St. Jules
c31e41f57a Merge pull request #45 from GrahamCampbell/cs
CS Fixes
2014-09-27 09:25:57 -07:00
Graham Campbell
722f30d132 CS fixes 2014-09-27 17:23:34 +01:00
Daniel St. Jules
26a0fed52e Merge and fix containsAll 2014-09-27 09:07:37 -07:00
Daniel St. Jules
1c0026ca00 Cleanup containsAny 2014-09-27 08:42:51 -07:00
Lucas Michot
da5ee7057d Add containsAll 2014-09-23 23:51:27 +02:00
Lucas Michot
410ceac663 Add containsAny 2014-09-23 23:48:28 +02:00
Daniel St. Jules
857650f278 Merge pull request #42 from lucasmichot/assertStringy
Add assertStringy shortcut
2014-09-23 14:28:05 -07:00
Lucas Michot
bc11fce93e Add assertStringy shortcut 2014-09-23 22:48:14 +02:00
Daniel St. Jules
fc885d777d Merge pull request #38 from lucasmichot/psr2
Small PSR-2 fixes
2014-09-23 08:49:03 -07:00
Lucas Michot
9d2863b983 Go psr-2 2014-09-23 17:40:58 +02:00
Daniel St. Jules
037184e3d9 Updated intro in README 2014-09-14 23:01:46 -07:00
Daniel St. Jules
8ac1918386 Updated travis ci badge 2014-09-14 11:07:24 -07:00
Daniel St. Jules
7cb6709fd4 Merge pull request #33 from GrahamCampbell/patch-1
Improved the suggested version constraint
2014-09-14 11:05:17 -07:00
Graham Campbell
a663802c2c Improved the suggested version constraint 2014-09-14 19:02:49 +01:00
Daniel St. Jules
7e91d1ead2 1.6.0 2014-09-14 10:58:46 -07:00
Daniel St. Jules
abf3e38c1c Updated logo 2014-09-14 10:49:26 -07:00
Daniel St. Jules
5a5c39d02b Fix readme 2014-09-14 09:49:26 -07:00
Daniel St. Jules
c577684e0a Renamed toTitleCase 2014-09-14 09:27:46 -07:00
Lucas Michot
681463de87 Add casetitle function 2014-09-14 14:16:09 +02:00
Daniel St. Jules
90de5cca00 Merge pull request #29 from GrahamCampbell/patch-1
Update .travis.yml
2014-07-09 22:21:03 -04:00
Graham Campbell
a0b7615210 Update .travis.yml 2014-07-09 18:18:00 +01:00
Daniel St. Jules
63ad4d238f 1.5.2 release 2014-07-09 13:13:57 -04:00
Daniel St. Jules
ece219b609 Add note on HHVM support to readme 2014-07-09 13:11:48 -04:00
Daniel St. Jules
e81dbaa6d9 Disable allowing failures for 5.6 and hhvm 2014-05-18 21:02:29 -04:00
Daniel St. Jules
2b2c03bd1f Add 5.6 and hhvm to .travis.yml 2014-05-18 20:59:16 -04:00
Daniel St. Jules
7600549abc 1.5.1 release 2014-04-19 11:03:05 -04:00
Daniel St. Jules
8fe30d18b7 Fix toAscii failing to remove remaining non-ascii characters 2014-04-19 00:49:46 -04:00
Daniel St. Jules
ae7527ad4a Remove leading or trailing replacement from slugified string, if present 2014-04-19 00:33:59 -04:00
Yuri Druzhkov
f74f535b78 add preg_quote, add test on regex special symbols 2014-04-15 10:09:11 +04:00
Yuri Druzhkov
5c5087db13 dashes fixes 2014-04-01 18:03:48 +04:00
Daniel St. Jules
8d961e25d0 1.5.0 release 2014-03-19 19:25:00 -04:00
Daniel St. Jules
e46797a9f8 Updated pad methods 2014-03-19 00:40:36 -04:00
Daniel St. Jules
f0805f522d Merge branch 'pr/20' after cleanup 2014-03-18 23:36:52 -04:00
Bilge
6103eca714 Rewrote Stringy::pad to be DRY.
Fixed "Class 'CommonTest' not found" error when running specific tests in PHPUnit by including tests directory in Composer's autoloader.
2014-03-18 23:34:54 -04:00
Daniel St. Jules
952ffd4c00 Add tests classmap to composer
As suggested by Bilge in https://github.com/danielstjules/Stringy/pull/20
2014-03-09 20:52:18 -04:00
Daniel St. Jules
7832e22e09 Update require paths in tests 2014-03-09 20:12:53 -04:00
Daniel St. Jules
bcd87c69b8 Merge pull request #26 from lucasmichot/patch-1
Add PHPUnit 4.0.* to require-dev
2014-03-09 20:07:23 -04:00
Lucas
1d9001bcf0 Add PHPUnit 4.0.* to require-dev 2014-03-07 13:45:59 +01:00
Daniel St. Jules
ee88cd93e8 Rename protected method slug to applyDelimeter, fix typos in comments 2014-03-05 19:34:09 -05:00
Daniel St. Jules
48f53e98fa Merge pull request #25 from BastianHofmann/patch-1
DRY underscored and dasherize methods
2014-03-05 19:24:34 -05:00
Daniel St. Jules
6e7415a9f5 Merge pull request #24 from maximal/patch-1
Remove conflicting transliteration for Bulgarian
2014-03-05 19:23:16 -05:00
BastianHofmann
c62a5e8d4e DRY underscored and dasherize methods 2014-03-05 11:21:21 +01:00
MaximAL
cd41723b12 Update CommonTest.php 2014-02-19 10:43:06 +03:00
Daniel St. Jules
404909ffaa Fixes #19: Handle consecutive invalid chars in slugify 2014-02-17 11:22:59 -05:00
Daniel St. Jules
210c928b79 Added links to readme, included SliceableStringy 2014-02-17 10:41:38 -05:00
Daniel St. Jules
1965da2e39 Made str and encoding protected, added getEncoding
Neither should be private, so as to allow users to easily extend the
class
2014-02-16 21:22:32 -05:00
MaximAL
25fa7340ed Hard sign in Russian is not equivalent to a, it’s rather '
Apostrophe is also most common transliteration character for soft sign (ь).
2014-02-16 18:50:29 +03:00
Daniel St. Jules
e5f66c6eca Merge pull request #21 from Bilge/dry-regex-replace
Optimized replace() and collapseWhitespace() by reusing regexReplace().
2014-02-14 07:09:57 -05:00
Bilge
5d7e9a8d36 Optimized replace() and collapseWhitespace() by reusing regexReplace().
Fixed bug in replace() where replacement string was being escaped with preg_quote() and added matching tests.
2014-02-14 01:26:55 +00:00
Daniel St. Jules
a7c5b8721c Add composer.lock and .DS_Store to .gitignore 2014-02-13 13:29:57 -05:00
Daniel St. Jules
9399f7a694 Fix isJSON
Thanks to Drainedsoul
2014-02-13 06:32:54 -05:00
Daniel St. Jules
b990df3c61 1.4.0 release 2014-02-12 19:26:15 -05:00
Daniel St. Jules
220d4f95c0 Fix indentation in test - it was bugging me 2014-02-12 19:07:38 -05:00
Daniel St. Jules
b3e922c517 Start using PSR-4 2014-02-12 19:02:19 -05:00
Daniel St. Jules
edaf567a13 A bit of code cleanup 2014-02-11 22:55:26 -05:00
Daniel St. Jules
1d186ca79f Code comments and documentation cleanup 2014-02-10 23:36:51 -05:00
Daniel St. Jules
ffe6c2575a Add exceptions to ArrayAccess examples 2014-02-06 18:31:34 -05:00
Daniel St. Jules
3f5b6a2413 Update ArrayAccess interface implementation, add details to readme 2014-02-06 18:27:21 -05:00
Daniel St. Jules
ebc58a51ef Stringy\Stringy now implements the ArrayAccess interface 2014-02-06 00:07:20 -05:00
Daniel St. Jules
1c13f2ad7e Renamed count to countSubstr, implemented Countable interface 2014-02-03 23:41:57 -05:00
Daniel St. Jules
7313dcc07e Fix failing tests, remove PHP 5.4 short array syntax 2014-02-02 16:10:06 -05:00
Daniel St. Jules
f14e8c16a4 Implemented IteratorAggregate, added chars() 2014-02-02 16:07:47 -05:00
Daniel St. Jules
9f930262af Update changelog for 1.3.0 2013-12-16 21:04:30 -05:00
Daniel St. Jules
c230885a13 Require objects to have a __toString method, update readme 2013-12-15 01:08:23 -05:00
Daniel St. Jules
1f25d0c793 Merge Pull Request #18 2013-12-15 00:15:34 -05:00
Daniel St. Jules
0e70d72352 Merge pull request #16 from ivank/patch-2
Support Bulgarian in toAscii
2013-12-13 06:32:34 -08:00
Ivan Kerin
d54628e6ae Support Bulgarian in toAscii
The “proper” bulgarian translitaration should encode ь to y [1],
However, since its not really that important to understand the actual
word, very rearly used and breaks compatibility with Russian, it might
be best to just leave it be, since Russian is a much larger torget.

[1] http://en.wikipedia.org/wiki/Romanization_of_Bulgarian
2013-12-13 11:05:23 +00:00
gotterdemarung
7335ee5c88 fixed T_OBJECT_OPERATOR issue in PHP 5.3 2013-12-10 23:10:44 +02:00
gotterdemarung
5e01d895db titleize refactoring to avoid visibility problems in PHP 5.3 2013-12-10 22:59:10 +02:00
gotterdemarung
3458fd79fd Stringy->$str became private to ensure its sting content and guarantee zero-exception __toString() execution 2013-12-10 22:34:19 +02:00
gotterdemarung
6e5e5e055f primitives check in constructor & toString 2013-12-10 22:19:42 +02:00
gotterdemarung
88d5973701 Implicit cast argument to string in constructor 2013-12-10 22:15:58 +02:00
gotterdemarung
75404a0338 Stringy must correctly handle non-string arguments and implicitly cast to string to avoid problems in __toString() method 2013-12-10 22:15:28 +02:00
Daniel St. Jules
bc96cdad0c Merge pull request #17 from floriansemm/master
fix typo in pad function call
2013-12-09 20:55:26 -08:00
Florian Semm
6014ff52df fix typo in pad function call 2013-12-09 15:15:45 +01:00
Daniel St. Jules
6ba29637b2 Updated changelog for 1.2.2 2013-12-04 23:36:17 -05:00
Daniel St. Jules
2ada00ff62 Add logo 2013-12-01 15:43:03 -05:00
Daniel St. Jules
2a197244a5 Update readme and comments for new slugify param 2013-11-27 01:14:09 -05:00
Daniel St. Jules
8b1a91fe0c Merge pull request #15 from glynnforrest/slug
Adding optional $replacement parameter to slugify.
2013-11-26 22:03:02 -08:00
Glynn Forrest
906f217dc1 Adding optional $replacement parameter to slugify. 2013-11-26 14:11:41 +00:00
Daniel St. Jules
d4baab7583 Merge pull request #14 from sergebezborodov/master
Use late static bindings in Stringy::create
2013-11-05 20:00:18 -08:00
Serge Bezborodov
0c8f0e9083 changes in create function 2013-11-01 09:55:38 +02:00
Daniel St. Jules
2155447ce8 Updated changelog for 1.2.1 2013-10-11 01:19:53 -04:00
Daniel St. Jules
77efce3a38 Added homepage to composer.json 2013-10-11 01:17:17 -04:00
Daniel St. Jules
f6085ed2cb Cleanup tests a bit
Getting over my irrational fear of multi-line return statements
2013-09-27 20:13:04 -04:00
Daniel St. Jules
fa36291254 Updated changelog for 1.2.0 2013-09-15 00:17:51 -04:00
Daniel St. Jules
88145e83e8 Update replace() to correctly apply preg_quote to pattern and replacement 2013-09-14 23:59:46 -04:00
Daniel St. Jules
9bd5a9c0c8 Added regexReplace() 2013-09-14 23:45:18 -04:00
Daniel St. Jules
41ea0277b2 Added $caseSensitive to count() 2013-09-14 14:58:11 -04:00
Daniel St. Jules
d862f9c24c Fix toUpperCase and toLowerCase not being static in StaticStringy 2013-09-14 11:31:51 -04:00
Daniel St. Jules
61cd5f5f4d Added toLowerCase() and toUpperCase() 2013-09-14 11:22:52 -04:00
Daniel St. Jules
ea20aebc91 Added $caseSensitive to contains() 2013-09-14 01:32:52 -04:00
Daniel St. Jules
49e1c5ad2a Merge pull request #13 from pborreli/typos
Closes Issue 13: Fixed typo in PHPDoc
2013-09-06 05:18:22 -07:00
Pascal Borreli
90913d39bc Fixed typos/PHPDoc 2013-09-06 08:44:42 +01:00
Daniel St. Jules
02da00c433 Merge branch 'pr/12' 2013-09-05 22:41:19 -04:00
Borales
07f23d6165 Adding cyrillic letters 2013-09-04 18:23:50 +03:00
Daniel St. Jules
d27683b570 Merge pull request #10 from ChristianGaertner/patch-2
Issue 10: Fixed Exception in pad() and added test coverage
2013-09-04 05:38:41 -07:00
ChristianGaertner
bc175f0e12 Added test to cover 100% + fixed fatal error 2013-09-04 08:02:56 +02:00
Daniel St. Jules
a6964e4561 Merge pull request #9 from ChristianGaertner/patch-1
Wrapped some things into code blocks
2013-09-03 14:13:28 -07:00
ChristianGaertner
5fff1a4124 Added codeblocks 2013-09-03 22:51:50 +02:00
15 changed files with 4273 additions and 2290 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
vendor/
composer.lock
.DS_Store

View File

@@ -1,5 +1,7 @@
language: php
php:
- 5.6
- 5.5
- 5.4
- 5.3
- hhvm

View File

@@ -1,3 +1,102 @@
### 1.10.0 (2015-07-22)
* Added trimLeft, trimRight
* Added support for unicode whitespace to trim
* Added delimit
* Added indexOf and indexOfLast
* Added htmlEncode and htmlDecode
* Added "Ç" in toAscii()
### 1.9.0 (2015-02-09)
* Added hasUpperCase and hasLowerCase
* Added $removeUnsupported parameter to toAscii()
* Improved toAscii support with additional Unicode spaces, Vietnamese chars,
and numerous other characters
* Separated the charsArray from toAscii as a protected method that may be
extended by inheriting classes
* Chars array is cached for better performance
### 1.8.1 (2015-01-08)
* Optimized chars()
* Added "ä Ä Ö Ü"" in toAscii()
* Added support for Unicode spaces in toAscii()
* Replaced instances of self::create() with static::create()
* Added missing test cases for safeTruncate() and longestCommonSuffix()
* Updated Stringy\create() to avoid collision when it already exists
### 1.8.0 (2015-01-03)
* Listed ext-mbstring in composer.json
* Added Stringy\create function for PHP 5.6
### 1.7.0 (2014-10-14)
* Added containsAll and containsAny
* Light cleanup
### 1.6.0 (2014-09-14)
* Added toTitleCase
### 1.5.2 (2014-07-09)
* Announced support for HHVM
### 1.5.1 (2014-04-19)
* Fixed toAscii() failing to remove remaining non-ascii characters
* Updated slugify() to treat dash and underscore as delimiters by default
* Updated slugify() to remove leading and trailing delimiter, if present
### 1.5.0 (2014-03-19)
* Made both str and encoding protected, giving property access to subclasses
* Added getEncoding()
* Fixed isJSON() giving false negatives
* Cleaned up and simplified: replace(), collapseWhitespace(), underscored(),
dasherize(), pad(), padLeft(), padRight() and padBoth()
* Fixed handling consecutive invalid chars in slugify()
* Removed conflicting hard sign transliteration in toAscii()
### 1.4.0 (2014-02-12)
* Implemented the IteratorAggregate interface, added chars()
* Renamed count() to countSubstr()
* Updated count() to implement Countable interface
* Implemented the ArrayAccess interface with positive and negative indices
* Switched from PSR-0 to PSR-4 autoloading
### 1.3.0 (2013-12-16)
* Additional Bulgarian support for toAscii
* str property made private
* Constructor casts first argument to string
* Constructor throws an InvalidArgumentException when given an array
* Constructor throws an InvalidArgumentException when given an object without
a __toString method
### 1.2.2 (2013-12-04)
* Updated create function to use late static binding
* Added optional $replacement param to slugify
### 1.2.1 (2013-10-11)
* Cleaned up tests
* Added homepage to composer.json
### 1.2.0 (2013-09-15)
* Fixed pad's use of InvalidArgumentException
* Fixed replace(). It now correctly treats regex special chars as normal chars
* Added additional Cyrillic letters to toAscii
* Added $caseSensitive to contains() and count()
* Added toLowerCase()
* Added toUpperCase()
* Added regexReplace()
### 1.1.0 (2013-08-31)
* Fix for collapseWhitespace()

471
README.md
View File

@@ -1,24 +1,39 @@
# Stringy
![Stringy](http://danielstjules.com/github/stringy-logo.png)
A PHP library with a variety of string manipulation functions with multibyte support. Offers both OO method chaining and a procedural-style static wrapper. Compatible with PHP 5.3+. Inspired by underscore.string.js.
A PHP string manipulation library with multibyte support. Offers both OO method
chaining and a procedural-style static wrapper. Tested and compatible with
PHP 5.3+ and HHVM. Inspired by underscore.string.js.
[![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy)
[![Build Status](https://api.travis-ci.org/danielstjules/Stringy.svg?branch=master)](https://travis-ci.org/danielstjules/Stringy)
* [Requiring/Loading](#requiringloading)
* [OO and Procedural](#oo-and-procedural)
* [Implemented Interfaces](#implemented-interfaces)
* [PHP 5.6 Creation](#php-56-creation)
* [Methods](#methods)
* [at](#at)
* [camelize](#camelize)
* [chars](#chars)
* [collapseWhitespace](#collapsewhitespace)
* [contains](#contains)
* [count](#count)
* [containsAll](#containsall)
* [containsAny](#containsany)
* [countSubstr](#countsubstr)
* [create](#create)
* [dasherize](#dasherize)
* [delimit](#delimit)
* [endsWith](#endswith)
* [ensureLeft](#ensureleft)
* [ensureRight](#ensureright)
* [first](#first)
* [getEncoding](#getencoding)
* [hasLowerCase](#haslowercase)
* [hasUpperCase](#hasuppercase)
* [htmlDecode](#htmldecode)
* [htmlEncode](#htmlencode)
* [humanize](#humanize)
* [indexOf](#indexof)
* [indexOfLast](#indexoflast)
* [insert](#insert)
* [isAlpha](#isalpha)
* [isAlphanumeric](#isalphanumeric)
@@ -38,6 +53,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [padBoth](#padboth)
* [padLeft](#padleft)
* [padRight](#padright)
* [regexReplace](#regexreplace)
* [removeLeft](#removeleft)
* [removeRight](#removeright)
* [replace](#replace)
@@ -52,25 +68,37 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [tidy](#tidy)
* [titleize](#titleize)
* [toAscii](#toascii)
* [toLowerCase](#tolowercase)
* [toSpaces](#tospaces)
* [toTabs](#totabs)
* [toTitleCase](#totitlecase)
* [toUpperCase](#touppercase)
* [trim](#trim)
* [trimLeft](#trimLeft)
* [trimRight](#trimRight)
* [truncate](#truncate)
* [underscored](#underscored)
* [upperCamelize](#uppercamelize)
* [upperCaseFirst](#uppercasefirst)
* [Links](#links)
* [Tests](#tests)
* [License](#license)
## Requiring/Loading
If you're using Composer to manage dependencies, you can include the following in your composer.json file:
If you're using Composer to manage dependencies, you can include the following
in your composer.json file:
```json
{
"require": {
"danielstjules/stringy": "dev-master"
"danielstjules/stringy": "~1.10"
}
}
```
Then, after running `composer update` or `php composer.phar update`, you can load the class using Composer's autoloading:
Then, after running `composer update` or `php composer.phar update`, you can
load the class using Composer's autoloading:
```php
require 'vendor/autoload.php';
@@ -79,9 +107,9 @@ require 'vendor/autoload.php';
Otherwise, you can simply require the file directly:
```php
require_once 'path/to/Stringy/src/Stringy/Stringy.php';
require_once 'path/to/Stringy/src/Stringy.php';
// or
require_once 'path/to/Stringy/src/Stringy/StaticStringy.php';
require_once 'path/to/Stringy/src/StaticStringy.php';
```
And in either case, I'd suggest using an alias.
@@ -103,9 +131,9 @@ use Stringy\Stringy as S;
echo S::create('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
```
`Stringy\Stringy` contains a __toString() method, which returns the current
string when the object is used in a string context. Its $str property is also
public, and can be accessed directly if required, ie: `S::create('foo')->str // 'foo'`
`Stringy\Stringy` has a __toString() method, which returns the current string
when the object is used in a string context, ie:
`(string) S::create('foo') // 'foo'`
Using the static wrapper, an alternative is the following:
@@ -115,20 +143,75 @@ $string = S::collapseWhitespace('Fòô Bàř', 'UTF-8');
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ'
```
## Implemented Interfaces
`Stringy\Stringy` implements the `IteratorAggregate` interface, meaning that
`foreach` can be used with an instance of the class:
``` php
$stringy = S::create('Fòô Bàř', 'UTF-8');
foreach ($stringy as $char) {
echo $char;
}
// 'Fòô Bàř'
```
It implements the `Countable` interface, enabling the use of `count()` to
retrieve the number of characters in the string:
``` php
$stringy = S::create('Fòô', 'UTF-8');
count($stringy); // 3
```
Furthermore, the `ArrayAccess` interface has been implemented. As a result,
`isset()` can be used to check if a character at a specific index exists. And
since `Stringy\Stringy` is immutable, any call to `offsetSet` or `offsetUnset`
will throw an exception. `offsetGet` has been implemented, however, and accepts
both positive and negative indexes. Invalid indexes result in an
`OutOfBoundsException`.
``` php
$stringy = S::create('Bàř', 'UTF-8');
echo $stringy[2]; // 'ř'
echo $stringy[-2]; // 'à'
isset($stringy[-4]); // false
$stringy[3]; // OutOfBoundsException
$stringy[2] = 'a'; // Exception
```
## PHP 5.6 Creation
As of PHP 5.6, [`use function`](https://wiki.php.net/rfc/use_function) is
available for importing functions. Stringy exposes a namespaced function,
`Stringy\create`, which emits the same behaviour as `Stringy\Stringy::create()`.
If running PHP 5.6, or another runtime that supports the `use function` syntax,
you can take advantage of an even simpler API as seen below:
``` php
use function Stringy\create as s;
// Instead of: S::create('Fòô Bàř', 'UTF-8')
s('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase();
```
## Methods
In the list below, any static method other than S::create refers to a
method in Stringy\StaticStringy. For all others, they're found in Stringy\Stringy. Furthermore, all methods that return a Stringy object or string do not modify the original.
In the list below, any static method other than S::create refers to a method in
`Stringy\StaticStringy`. For all others, they're found in `Stringy\Stringy`.
Furthermore, all methods that return a Stringy object or string do not modify
the original. Stringy objects are immutable.
*Note: If $encoding is not given, it defaults to mb_internal_encoding().*
*Note: If `$encoding` is not given, it defaults to `mb_internal_encoding()`.*
#### at
$stringy->at(int $index)
S::substr(int $index [, string $encoding ])
S::at(int $index [, string $encoding ])
Returns the character of the string at $index, with indexes starting at 0.
Returns the character at $index, with indexes starting at 0.
```php
S::create('fòô bàř', 'UTF-8')->at(6);
@@ -141,15 +224,28 @@ $stringy->camelize();
S::camelize(string $str [, string $encoding ])
Returns a camelCase version of the supplied string. Trims surrounding
spaces, capitalizes letters following digits, spaces, dashes and
underscores, and removes spaces, dashes, underscores.
Returns a camelCase version of the string. Trims surrounding spaces,
capitalizes letters following digits, spaces, dashes and underscores,
and removes spaces, dashes, as well as underscores.
```php
S::create('Camel-Case')->camelize();
S::camelize('Camel-Case'); // 'camelCase'
```
#### chars
$stringy->chars();
S::chars(string $str [, string $encoding ])
Returns an array consisting of the characters in the string.
```php
S::create('Fòô Bàř', 'UTF-8')->chars();
S::chars('Fòô Bàř', 'UTF-8'); // array(F', 'ò', 'ô', ' ', 'B', 'à', 'ř')
```
#### collapseWhitespace
$stringy->collapseWhitespace()
@@ -167,38 +263,73 @@ S::collapseWhitespace(' Ο συγγραφέας '); // 'Ο συγγρα
#### contains
$stringy->contains(string $needle)
$stringy->contains(string $needle [, boolean $caseSensitive = true ])
S::contains(string $haystack, string $needle [, string $encoding ])
S::contains(string $haystack, string $needle [, boolean $caseSensitive = true [, string $encoding ]])
Returns true if the string contains $needle, false otherwise.
Returns true if the string contains $needle, false otherwise. By default,
the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php
S::create('Ο συγγραφέας είπε', 'UTF-8')->contains('συγγραφέας');
S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'); // true
```
#### count
#### containsAll
$stringy->count(string $substring)
$stringy->containsAll(array $needles [, boolean $caseSensitive = true ])
S::count(string $str, string $substring [, string $encoding ])
S::containsAll(string $haystack, array $needles [, boolean $caseSensitive = true [, string $encoding ]])
Returns the number of occurrences of $substring in the given string. An alias for
mb_substr_count()
Returns true if the string contains all $needles, false otherwise. By
default the comparison is case-sensitive, but can be made insensitive by
setting $caseSensitive to false.
```php
S::create('Ο συγγραφέας είπε', 'UTF-8')->count('α');
S::count('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2
S::create('Str contains foo and bar')->containsAll(array('foo', 'bar'));
S::containsAll('Str contains foo and bar', array('foo', 'bar')); // true
```
#### containsAny
$stringy->containsAny(array $needles [, boolean $caseSensitive = true ])
S::containsAny(string $haystack, array $needles [, boolean $caseSensitive = true [, string $encoding ]])
Returns true if the string contains any $needles, false otherwise. By
default the comparison is case-sensitive, but can be made insensitive by
setting $caseSensitive to false.
```php
S::create('Str contains foo')->containsAny(array('foo', 'bar'));
S::containsAny('Str contains foo', array('foo', 'bar')); // true
```
#### countSubstr
$stringy->countSubstr(string $substring [, boolean $caseSensitive = true ])
S::countSubstr(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
Returns the number of occurrences of $substring in the given string.
By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php
S::create('Ο συγγραφέας είπε', 'UTF-8')->countSubstr('α');
S::countSubstr('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2
```
#### create
S::create(string $str, [, $encoding ])
S::create(mixed $str [, $encoding ])
Creates a Stringy object and assigns both str and encoding properties
the supplied values. If $encoding is not specified, it defaults to
mb_internal_encoding(). It then returns the initialized object.
the supplied values. $str is cast to a string prior to assignment, and if
$encoding is not specified, it defaults to mb_internal_encoding(). It
then returns the initialized object. Throws an InvalidArgumentException
if the first argument is an array or object without a __toString method.
```php
$stringy = S::create('fòô bàř', 'UTF-8'); // 'fòô bàř'
@@ -219,6 +350,22 @@ S::create('TestDCase')->dasherize();
S::dasherize('TestDCase'); // 'test-d-case'
```
#### delimit
$stringy->delimit($delimiter);
S::delimit(string $str [, string $delimiter, string $encoding ])
Returns a lowercase and trimmed string separated by the given delimiter.
Delimiters are inserted before uppercase characters (with the exception
of the first character of the string), and in place of spaces, dashes,
and underscores. Alpha delimiters are not converted to lowercase.
```php
S::create('TestDCase')->delimit('>>');
S::delimit('TestCase', '>>'); // 'test>>case'
```
#### endsWith
$stringy->endsWith(string $substring [, boolean $caseSensitive = true ])
@@ -273,6 +420,68 @@ S::create('fòô bàř', 'UTF-8')->first(3);
S::first('fòô bàř', 3, 'UTF-8'); // 'fòô'
```
#### getEncoding
$stringy->getEncoding()
Returns the encoding used by the Stringy object.
```php
S::create('fòô bàř', 'UTF-8')->getEncoding(); // 'UTF-8'
```
#### hasLowerCase
$stringy->hasLowerCase()
S::hasLowerCase(string $str [, string $encoding ])
Returns true if the string contains a lower case char, false otherwise.
```php
S::create('fòô bàř', 'UTF-8')->hasLowerCase();
S::hasLowerCase('fòô bàř', 'UTF-8'); // true
```
#### hasUpperCase
$stringy->hasUpperCase()
S::hasUpperCase(string $str [, string $encoding ])
Returns true if the string contains an upper case char, false otherwise.
```php
S::create('fòô bàř', 'UTF-8')->hasUpperCase();
S::hasUpperCase('fòô bàř', 'UTF-8'); // false
```
#### htmlDecode
$stringy->htmlDecode()
S::htmlDecode(string $str [, int $flags, string $encoding ])
Convert all HTML entities to their applicable characters.
```php
S::create('&amp;')->htmlDecode();
S::htmlDecode('&amp;'); // '&'
```
#### htmlEncode
$stringy->htmlEncode()
S::htmlEncode(string $str [, int $flags, string $encoding ])
Convert all applicable characters to HTML entities.
```php
S::create('&')->htmlEncode();
S::htmlEncode('&'); // '&amp;'
```
#### humanize
$stringy->humanize()
@@ -287,6 +496,36 @@ S::create('author_id')->humanize();
S::humanize('author_id'); // 'Author'
```
#### indexOf
$stringy->indexOf(string $needle [, $offset = 0 ]);
S::indexOf(string $haystack , string $needle [, $offset = 0 [, $encoding = null ]])
Returns the index of the first occurrence of $needle in the string,
and false if not found. Accepts an optional offset from which to begin
the search.
```php
S::create('string', 'UTF-8')->indexOf('ing');
S::indexOf('string', 'ing'); // 3
```
#### indexOfLast
$stringy->indexOfLast(string $needle [, $offset = 0 ]);
S::indexOfLast(string $haystack , string $needle [, $offset = 0 [, $encoding = null ]])
Returns the index of the last occurrence of $needle in the string,
and false if not found. Accepts an optional offset from which to begin
the search.
```php
S::create('string', 'UTF-8')->indexOfLast('ing');
S::indexOfLast('string string', 'ing'); // 10
```
#### insert
$stringy->insert(int $index, string $substring)
@@ -497,7 +736,7 @@ string used for padding is a space, and the default type (one of 'left',
$padType isn't one of those 3 values.
```php
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left',);
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left');
S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř'
```
@@ -543,13 +782,29 @@ S::create('foo bar')->padRight(10, '_*');
S::padRight('foo bar', 10, '_*'); // 'foo bar_*_'
```
#### regexReplace
$stringy->regexReplace(string $pattern, string $replacement [, string $options = 'msr'])
S::regexReplace(string $str, string $pattern, string $replacement [, string $options = 'msr' [, string $encoding ]])
Replaces all occurrences of $pattern in $str by $replacement. An alias
for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
in the bundled version of Oniguruma in PHP 5.3.
```php
S::create('fòô ', 'UTF-8')->regexReplace('f[òô]+\s', 'bàř', 'msr');
S::regexReplace('fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'); // 'bàř'
```
#### removeLeft
$stringy->removeLeft(string $substring)
S::removeLeft(string $str, string $substring [, string $encoding ])
Returns a new string with the prefix $substring removed, if it was present.
Returns a new string with the prefix $substring removed, if present.
```php
S::create('fòô bàř', 'UTF-8')->removeLeft('fòô ');
@@ -562,7 +817,7 @@ $stringy->removeRight(string $substring)
S::removeRight(string $str, string $substring [, string $encoding ])
Returns a new string with the suffix $substring removed, if it was present.
Returns a new string with the suffix $substring removed, if present.
```php
S::create('fòô bàř', 'UTF-8')->removeRight(' bàř');
@@ -571,11 +826,11 @@ S::removeRight('fòô bàř', ' bàř', 'UTF-8'); // 'fòô'
#### replace
$stringy->replace(string $search, string $replace)
$stringy->replace(string $search, string $replacement)
S::replace(string $str, string $search, string $replace [, string $encoding ])
S::replace(string $str, string $search, string $replacement [, string $encoding ])
Returns a string with all occurrences of $search replaced with $replace.
Replaces all occurrences of $search in $str by $replacement.
```php
S::create('fòô bàř fòô bàř', 'UTF-8')->replace('fòô ', '');
@@ -586,7 +841,7 @@ S::replace('fòô bàř fòô bàř', 'fòô ', '', 'UTF-8'); // 'bàř bàř'
$stringy->reverse()
S::reverse(string $str, [, string $encoding ])
S::reverse(string $str [, string $encoding ])
Returns a reversed string. A multibyte version of strrev().
@@ -597,13 +852,13 @@ S::reverse('fòô bàř', 'UTF-8'); // 'řàb ôòf'
#### safeTruncate
$stringy->safeTruncate(int $length, [, string $substring = '' ])
$stringy->safeTruncate(int $length [, string $substring = '' ])
S::safeTruncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
S::safeTruncate(string $str, int $length [, string $substring = '' [, string $encoding ]])
Truncates the string to a given length, while ensuring that it does not
chop words. If $substring is provided, and truncating occurs, the string
is further truncated so that the substring may be appended without
split words. If $substring is provided, and truncating occurs, the
string is further truncated so that the substring may be appended without
exceeding the desired length.
```php
@@ -617,7 +872,8 @@ $stringy->shuffle()
S::shuffle(string $str [, string $encoding ])
A multibyte str_shuffle() function. It returns a string with its characters in random order.
A multibyte str_shuffle() function. It returns a string with its characters in
random order.
```php
S::create('fòô bàř', 'UTF-8')->shuffle();
@@ -626,13 +882,14 @@ S::shuffle('fòô bàř', 'UTF-8'); // 'àôřb òf'
#### slugify
$stringy->slugify()
$stringy->slugify([ string $replacement = '-' ])
S::slugify(string $str)
S::slugify(string $str [, string $replacement = '-' ])
Converts the string into an URL slug. This includes replacing non-ASCII
characters with their closest ASCII equivalents, removing non-alphanumeric
and non-ASCII characters, and replacing whitespace with dashes. The string
characters with their closest ASCII equivalents, removing remaining
non-ASCII and non-alphanumeric characters, and replacing whitespace with
$replacement. The replacement defaults to a single dash, and the string
is also converted to lowercase.
```php
@@ -717,9 +974,8 @@ $stringy->titleize([ string $encoding ])
S::titleize(string $str [, array $ignore [, string $encoding ]])
Returns a trimmed string with the first letter of each word capitalized.
Ignores the case of other letters, allowing for the use of acronyms.
Also accepts an array, $ignore, allowing you to list words not to be
capitalized.
Ignores the case of other letters, preserving any acronyms. Also accepts
an array, $ignore, allowing you to list words not to be capitalized.
```php
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
@@ -732,21 +988,36 @@ S::titleize('i like to watch DVDs at home', $ignore, 'UTF-8');
$stringy->toAscii()
S::toAscii(string $str)
S::toAscii(string $str [, boolean $removeUnsupported = true])
Returns an ASCII version of the string. A set of non-ASCII characters are
replaced with their closest ASCII counterparts, and the rest are removed.
replaced with their closest ASCII counterparts, and the rest are removed
unless instructed otherwise.
```php
S::create('fòô bàř')->toAscii();
S::toAscii('fòô bàř'); // 'foo bar'
```
#### toLowerCase
$stringy->toLowerCase()
S::toLowerCase(string $str [, string $encoding ])
Converts all characters in the string to lowercase. An alias for PHP's
mb_strtolower().
```php
S::create('FÒÔ BÀŘ', 'UTF-8')->toLowerCase();
S::toLowerCase('FÒÔ BÀŘ', 'UTF-8'); // 'fòô bàř'
```
#### toSpaces
$stringy->toSpaces([ tabLength = 4 ])
S::toSpaces(string $str, [, int $tabLength = 4 ])
S::toSpaces(string $str [, int $tabLength = 4 ])
Converts each tab in the string to some number of spaces, as defined by
$tabLength. By default, each tab is converted to 4 consecutive spaces.
@@ -760,7 +1031,7 @@ S::toSpaces(' String speech = "Hi"'); // ' String speech = "Hi"'
$stringy->toTabs([ tabLength = 4 ])
S::toTabs(string $str, [, int $tabLength = 4 ])
S::toTabs(string $str [, int $tabLength = 4 ])
Converts each occurrence of some consecutive number of spaces, as defined
by $tabLength, to a tab. By default, each 4 consecutive spaces are
@@ -771,32 +1042,91 @@ S::create(' fòô bàř')->toTabs();
S::toTabs(' fòô bàř'); // ' fòô bàř'
```
#### trim
#### toTitleCase
$stringy->trim()
$stringy->toTitleCase()
S::trim(string $str)
S::toTitleCase(string $str [, string $encoding ])
Returns the trimmed string. An alias for PHP's trim() function.
Converts the first character of each word in the string to uppercase.
```php
S::create('fòô bàř', 'UTF-8')->trim();
S::trim(' fòô bàř '); // 'fòô bàř'
S::create('fòô bàř', 'UTF-8')->toTitleCase();
S::toTitleCase('fòô bàř', 'UTF-8'); // 'Fòô Bàř'
```
#### toUpperCase
$stringy->toUpperCase()
S::toUpperCase(string $str [, string $encoding ])
Converts all characters in the string to uppercase. An alias for PHP's
mb_strtoupper().
```php
S::create('fòô bàř', 'UTF-8')->toUpperCase();
S::toUpperCase('fòô bàř', 'UTF-8'); // 'FÒÔ BÀŘ'
```
#### trim
$stringy->trim([, string $chars])
S::trim(string $str [, string $chars [, string $encoding ]])
Returns a string with whitespace removed from the start and end of the
string. Supports the removal of unicode whitespace. Accepts an optional
string of characters to strip instead of the defaults.
```php
S::create(' fòô bàř ', 'UTF-8')->trim();
S::trim(' fòô bàř '); // 'fòô bàř'
```
#### trimLeft
$stringy->trimLeft([, string $chars])
S::trimLeft(string $str [, string $chars [, string $encoding ]])
Returns a string with whitespace removed from the start of the string.
Supports the removal of unicode whitespace. Accepts an optional
string of characters to strip instead of the defaults.
```php
S::create(' fòô bàř ', 'UTF-8')->trimLeft();
S::trimLeft(' fòô bàř '); // 'fòô bàř '
```
#### trimRight
$stringy->trimRight([, string $chars])
S::trimRight(string $str [, string $chars [, string $encoding ]])
Returns a string with whitespace removed from the end of the string.
Supports the removal of unicode whitespace. Accepts an optional
string of characters to strip instead of the defaults.
```php
S::create(' fòô bàř ', 'UTF-8')->trimRight();
S::trimRight(' fòô bàř '); // ' fòô bàř'
```
#### truncate
$stringy->truncate(int $length, [, string $substring = '' ])
$stringy->truncate(int $length [, string $substring = '' ])
S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
S::truncate(string $str, int $length [, string $substring = '' [, string $encoding ]])
Truncates the string to a given length. If $substring is provided, and
truncating occurs, the string is further truncated so that the substring
may be appended without exceeding the desired length.
```php
S::create('What are your plans today?')->safeTruncate(19, '...');
S::safeTruncate('What are your plans today?', 19, '...'); // 'What are your pl...'
S::create('What are your plans today?')->truncate(19, '...');
S::truncate('What are your plans today?', 19, '...'); // 'What are your pl...'
```
#### underscored
@@ -842,6 +1172,15 @@ S::create('σ test', 'UTF-8')->upperCaseFirst();
S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test'
```
## Links
The following is a list of libraries that extend Stringy:
* [SliceableStringy](https://github.com/danielstjules/SliceableStringy):
Python-like string slices in PHP
* [SubStringy](https://github.com/TCB13/SubStringy):
Advanced substring methods
## Tests
From the project directory, tests can be ran using `phpunit`

View File

@@ -5,6 +5,7 @@
"multibyte", "string", "manipulation", "utility", "methods", "utf-8",
"helpers", "utils", "utf"
],
"homepage": "https://github.com/danielstjules/Stringy",
"license": "MIT",
"authors": [
{
@@ -14,9 +15,21 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"support": {
"issues": "https://github.com/danielstjules/Stringy/issues",
"source": "https://github.com/danielstjules/Stringy"
},
"autoload": {
"psr-0": { "Stringy": "src/" }
"psr-4": { "Stringy\\": "src/" },
"files": ["src/Create.php"]
},
"autoload-dev": {
"classmap": [ "tests" ]
}
}

View File

@@ -5,7 +5,10 @@
syntaxCheck="false">
<testsuites>
<testsuite name="Stringy">
<directory>tests/Stringy</directory>
<file>tests/CommonTest.php</file>
<file>tests/StringyTest.php</file>
<file>tests/StaticStringyTest.php</file>
<file phpVersion="5.6.0">tests/CreateTest.php</file>
</testsuite>
</testsuites>
</phpunit>

19
src/Create.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
namespace Stringy;
if (!function_exists('Stringy\create')) {
/**
* Creates a Stringy object and returns it on success.
*
* @param mixed $str Value to modify, after being cast to string
* @param string $encoding The character encoding
* @return Stringy A Stringy object
* @throws \InvalidArgumentException if an array or object without a
* __toString method is passed as the first argument
*/
function create($str, $encoding = null)
{
return new Stringy($str, $encoding);
}
}

979
src/StaticStringy.php Normal file
View File

@@ -0,0 +1,979 @@
<?php
namespace Stringy;
class StaticStringy
{
/**
* Returns an array consisting of the characters in the string.
*
* @param string $str String for which to return the chars
* @param string $encoding The character encoding
* @return array An array of string chars
*/
public static function chars($str, $encoding = null)
{
return Stringy::create($str, $encoding)->chars();
}
/**
* Converts the first character of the supplied string to upper case.
*
* @param string $str String to modify
* @param string $encoding The character encoding
* @return string String with the first character being upper case
*/
public static function upperCaseFirst($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->upperCaseFirst();
}
/**
* Converts the first character of the supplied string to lower case.
*
* @param string $str String to modify
* @param string $encoding The character encoding
* @return string String with the first character being lower case
*/
public static function lowerCaseFirst($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->lowerCaseFirst();
}
/**
* Returns a camelCase version of the string. Trims surrounding spaces,
* capitalizes letters following digits, spaces, dashes and underscores,
* and removes spaces, dashes, as well as underscores.
*
* @param string $str String to convert to camelCase
* @param string $encoding The character encoding
* @return string String in camelCase
*/
public static function camelize($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->camelize();
}
/**
* Returns an UpperCamelCase version of the supplied string. It trims
* surrounding spaces, capitalizes letters following digits, spaces, dashes
* and underscores, and removes spaces, dashes, underscores.
*
* @param string $str String to convert to UpperCamelCase
* @param string $encoding The character encoding
* @return string String in UpperCamelCase
*/
public static function upperCamelize($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->upperCamelize();
}
/**
* Returns a lowercase and trimmed string separated by dashes. Dashes are
* inserted before uppercase characters (with the exception of the first
* character of the string), and in place of spaces as well as underscores.
*
* @param string $str String to convert
* @param string $encoding The character encoding
* @return string Dasherized string
*/
public static function dasherize($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->dasherize();
}
/**
* Returns a lowercase and trimmed string separated by underscores.
* Underscores are inserted before uppercase characters (with the exception
* of the first character of the string), and in place of spaces as well as
* dashes.
*
* @param string $str String to convert
* @param string $encoding The character encoding
* @return string Underscored string
*/
public static function underscored($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->underscored();
}
/**
* Returns a lowercase and trimmed string separated by the given delimiter.
* Delimiters are inserted before uppercase characters (with the exception
* of the first character of the string), and in place of spaces, dashes,
* and underscores. Alpha delimiters are not converted to lowercase.
*
* @param string $str String to convert
* @param string $delimiter Sequence used to separate parts of the string
* @param string $encoding The character encoding
* @return string String with delimiter
*/
public static function delimit($str, $delimiter, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->delimit($delimiter);
}
/**
* Returns a case swapped version of the string.
*
* @param string $str String to swap case
* @param string $encoding The character encoding
* @return string String with each character's case swapped
*/
public static function swapCase($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->swapCase();
}
/**
* Returns a trimmed string with the first letter of each word capitalized.
* Ignores the case of other letters, preserving any acronyms. Also accepts
* an array, $ignore, allowing you to list words not to be capitalized.
*
* @param string $str String to titleize
* @param string $encoding The character encoding
* @param array $ignore An array of words not to capitalize
* @return string Titleized string
*/
public static function titleize($str, $ignore = null, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->titleize($ignore);
}
/**
* Capitalizes the first word of the string, replaces underscores with
* spaces, and strips '_id'.
*
* @param string $str String to humanize
* @param string $encoding The character encoding
* @return string A humanized string
*/
public static function humanize($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->humanize();
}
/**
* Returns a string with smart quotes, ellipsis characters, and dashes from
* Windows-1252 (commonly used in Word documents) replaced by their ASCII
* equivalents.
*
* @param string $str String to remove special chars
* @return string String with those characters removed
*/
public static function tidy($str)
{
return (string) Stringy::create($str)->tidy();
}
/**
* Trims the string and replaces consecutive whitespace characters with a
* single space. This includes tabs and newline characters, as well as
* multibyte whitespace such as the thin space and ideographic space.
*
* @param string $str The string to cleanup whitespace
* @param string $encoding The character encoding
* @return string The trimmed string with condensed whitespace
*/
public static function collapseWhitespace($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->collapseWhitespace();
}
/**
* Returns an ASCII version of the string. A set of non-ASCII characters are
* replaced with their closest ASCII counterparts, and the rest are removed
* unless instructed otherwise.
*
* @param string $str A string with non-ASCII characters
* @param bool $removeUnsupported Whether or not to remove the
* unsupported characters
* @return string A string containing only ASCII characters
*/
public static function toAscii($str, $removeUnsupported = true)
{
return (string) Stringy::create($str)->toAscii($removeUnsupported);
}
/**
* Pads the string to a given length with $padStr. If length is less than
* or equal to the length of the string, no padding takes places. The
* default string used for padding is a space, and the default type (one of
* 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException
* if $padType isn't one of those 3 values.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $padType One of 'left', 'right', 'both'
* @param string $encoding The character encoding
* @return string The padded string
* @throws \InvalidArgumentException If $padType isn't one of 'right',
* 'left' or 'both'
*/
public static function pad($str, $length, $padStr = ' ', $padType = 'right',
$encoding = null)
{
return (string) Stringy::create($str, $encoding)
->pad($length, $padStr, $padType);
}
/**
* Returns a new string of a given length such that the beginning of the
* string is padded. Alias for pad() with a $padType of 'left'.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $encoding The character encoding
* @return string The padded string
*/
public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
{
return (string) Stringy::create($str, $encoding)
->padLeft($length, $padStr);
}
/**
* Returns a new string of a given length such that the end of the string
* is padded. Alias for pad() with a $padType of 'right'.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $encoding The character encoding
* @return string The padded string
*/
public static function padRight($str, $length, $padStr = ' ', $encoding = null)
{
return (string) Stringy::create($str, $encoding)
->padRight($length, $padStr);
}
/**
* Returns a new string of a given length such that both sides of the
* string are padded. Alias for pad() with a $padType of 'both'.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $encoding The character encoding
* @return string The padded string
*/
public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
{
return (string) Stringy::create($str, $encoding)
->padBoth($length, $padStr);
}
/**
* Returns true if the string begins with $substring, false otherwise. By
* default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $str String to check the start of
* @param string $substring The substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $str starts with $substring
*/
public static function startsWith($str, $substring, $caseSensitive = true,
$encoding = null)
{
return Stringy::create($str, $encoding)
->startsWith($substring, $caseSensitive);
}
/**
* Returns true if the string ends with $substring, false otherwise. By
* default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $str String to check the end of
* @param string $substring The substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $str ends with $substring
*/
public static function endsWith($str, $substring, $caseSensitive = true,
$encoding = null)
{
return Stringy::create($str, $encoding)
->endsWith($substring, $caseSensitive);
}
/**
* Converts each tab in the string to some number of spaces, as defined by
* $tabLength. By default, each tab is converted to 4 consecutive spaces.
*
* @param string $str String to convert tabs to spaces
* @param int $tabLength Number of spaces to replace each tab with
* @return string String with tabs switched to spaces
*/
public static function toSpaces($str, $tabLength = 4)
{
return (string) Stringy::create($str)->toSpaces($tabLength);
}
/**
* Converts each occurrence of some consecutive number of spaces, as
* defined by $tabLength, to a tab. By default, each 4 consecutive spaces
* are converted to a tab.
*
* @param string $str String to convert spaces to tabs
* @param int $tabLength Number of spaces to replace with a tab
* @return string String with spaces switched to tabs
*/
public static function toTabs($str, $tabLength = 4)
{
return (string) Stringy::create($str)->toTabs($tabLength);
}
/**
* Converts all characters in the string to lowercase. An alias for PHP's
* mb_strtolower().
*
* @param string $str String to convert case
* @param string $encoding The character encoding
* @return string The lowercase string
*/
public static function toLowerCase($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->toLowerCase();
}
/**
* Converts the first character of each word in the string to uppercase.
*
* @param string $str String to convert case
* @param string $encoding The character encoding
* @return string The title-cased string
*/
public static function toTitleCase($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->toTitleCase();
}
/**
* Converts all characters in the string to uppercase. An alias for PHP's
* mb_strtoupper().
*
* @param string $str String to convert case
* @param string $encoding The character encoding
* @return string The uppercase string
*/
public static function toUpperCase($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->toUpperCase();
}
/**
* Converts the string into an URL slug. This includes replacing non-ASCII
* characters with their closest ASCII equivalents, removing remaining
* non-ASCII and non-alphanumeric characters, and replacing whitespace with
* $replacement. The replacement defaults to a single dash, and the string
* is also converted to lowercase.
*
* @param string $str Text to transform into an URL slug
* @param string $replacement The string used to replace whitespace
* @return string The corresponding URL slug
*/
public static function slugify($str, $replacement = '-')
{
return (string) Stringy::create($str)->slugify($replacement);
}
/**
* Returns true if the string contains $needle, false otherwise. By default,
* the comparison is case-sensitive, but can be made insensitive by setting
* $caseSensitive to false.
*
* @param string $haystack String being checked
* @param string $needle Substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $haystack contains $needle
*/
public static function contains($haystack, $needle, $caseSensitive = true,
$encoding = null)
{
return Stringy::create($haystack, $encoding)
->contains($needle, $caseSensitive);
}
/**
* Returns true if the string contains any $needles, false otherwise. By
* default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $haystack String being checked
* @param array $needles Substrings to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $haystack contains any $needles
*/
public static function containsAny($haystack, $needles,
$caseSensitive = true, $encoding = null)
{
return Stringy::create($haystack, $encoding)
->containsAny($needles, $caseSensitive);
}
/**
* Returns true if the string contains all $needles, false otherwise. By
* default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $haystack String being checked
* @param array $needles Substrings to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $haystack contains all $needles
*/
public static function containsAll($haystack, $needles,
$caseSensitive = true, $encoding = null)
{
return Stringy::create($haystack, $encoding)
->containsAll($needles, $caseSensitive);
}
/**
* Returns the index of the first occurrence of $needle in the string,
* and false if not found. Accepts an optional offset from which to begin
* the search.
*
* @param string $haystack String to search
* @param string $needle Substring to look for
* @param int $offset Offset from which to search
* @return int|bool The occurrence's index if found, otherwise false
*/
public static function indexOf($haystack, $needle, $offset = 0,
$encoding = null)
{
return Stringy::create($haystack, $encoding)
->indexOf($needle, $offset);
}
/**
* Returns the index of the last occurrence of $needle in the string,
* and false if not found. Accepts an optional offset from which to begin
* the search.
*
* @param string $haystack String to search
* @param string $needle Substring to look for
* @param int $offset Offset from which to search
* @return int|bool The last occurrence's index if found, otherwise false
*/
public static function indexOfLast($haystack, $needle, $offset = 0,
$encoding = null)
{
return Stringy::create($haystack, $encoding)
->indexOfLast($needle, $offset);
}
/**
* Surrounds a string with the given substring.
*
* @param string $str The string to surround
* @param string $substring The substring to add to both sides
* @return string The string with the substring prepended and appended
*/
public static function surround($str, $substring)
{
return (string) Stringy::create($str)->surround($substring);
}
/**
* Inserts $substring into the string at the $index provided.
*
* @param string $str String to insert into
* @param string $substring String to be inserted
* @param int $index The index at which to insert the substring
* @param string $encoding The character encoding
* @return string The resulting string after the insertion
*/
public static function insert($str, $substring, $index, $encoding = null)
{
return (string) Stringy::create($str, $encoding)
->insert($substring, $index);
}
/**
* Truncates the string to a given length. If $substring is provided, and
* truncating occurs, the string is further truncated so that the substring
* may be appended without exceeding the desired length.
*
* @param string $str String to truncate
* @param int $length Desired length of the truncated string
* @param string $substring The substring to append if it can fit
* @param string $encoding The character encoding
* @return string The resulting string after truncating
*/
public static function truncate($str, $length, $substring = '',
$encoding = null)
{
return (string) Stringy::create($str, $encoding)
->truncate($length, $substring);
}
/**
* Truncates the string to a given length, while ensuring that it does not
* split words. If $substring is provided, and truncating occurs, the
* string is further truncated so that the substring may be appended without
* exceeding the desired length.
*
* @param string $str String to truncate
* @param int $length Desired length of the truncated string
* @param string $substring The substring to append if it can fit
* @param string $encoding The character encoding
* @return string The resulting string after truncating
*/
public static function safeTruncate($str, $length, $substring = '',
$encoding = null)
{
return (string) Stringy::create($str, $encoding)
->safeTruncate($length, $substring);
}
/**
* Returns a reversed string. A multibyte version of strrev().
*
* @param string $str String to reverse
* @param string $encoding The character encoding
* @return string The reversed string
*/
public static function reverse($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->reverse();
}
/**
* A multibyte str_shuffle() function. It returns a string with its
* characters in random order.
*
* @param string $str String to shuffle
* @param string $encoding The character encoding
* @return string The shuffled string
*/
public static function shuffle($str, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->shuffle();
}
/**
* Returns a string with whitespace removed from the start and end of the
* string. Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults.
*
* @param string $str String to trim
* @param string $chars Optional string of characters to strip
* @param string $encoding The character encoding
* @return string Trimmed $str
*/
public static function trim($str, $chars = null, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->trim($chars);
}
/**
* Returns a string with whitespace removed from the start of the string.
* Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults.
*
* @param string $str String to trim
* @param string $chars Optional string of characters to strip
* @param string $encoding The character encoding
* @return string Trimmed $str
*/
public static function trimLeft($str, $chars = null, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->trimLeft($chars);
}
/**
* Returns a string with whitespace removed from the end of the string.
* Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults.
*
* @param string $str String to trim
* @param string $chars Optional string of characters to strip
* @param string $encoding The character encoding
* @return string Trimmed $str
*/
public static function trimRight($str, $chars = null, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->trimRight($chars);
}
/**
* Returns the longest common prefix between the string and $otherStr.
*
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
* @param string $encoding The character encoding
* @return string The longest common prefix
*/
public static function longestCommonPrefix($str, $otherStr, $encoding = null)
{
return (string) Stringy::create($str, $encoding)
->longestCommonPrefix($otherStr);
}
/**
* Returns the longest common suffix between the string and $otherStr.
*
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
* @param string $encoding The character encoding
* @return string The longest common suffix
*/
public static function longestCommonSuffix($str, $otherStr, $encoding = null)
{
return (string) Stringy::create($str, $encoding)
->longestCommonSuffix($otherStr);
}
/**
* Returns the longest common substring between the string and $otherStr.
* In the case of ties, it returns that which occurs first.
*
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
* @param string $encoding The character encoding
* @return string The longest common substring
*/
public static function longestCommonSubstring($str, $otherStr,
$encoding = null)
{
return (string) Stringy::create($str, $encoding)
->longestCommonSubstring($otherStr);
}
/**
* Returns the length of the string. An alias for PHP's mb_strlen() function.
*
* @param string $str The string to get the length of
* @param string $encoding The character encoding
* @return int The number of characters in $str given the encoding
*/
public static function length($str, $encoding = null)
{
return Stringy::create($str, $encoding)->length();
}
/**
* Returns the substring beginning at $start with the specified $length.
* It differs from the mb_substr() function in that providing a $length of
* null will return the rest of the string, rather than an empty string.
*
* @param string $str The string to get the length of
* @param int $start Position of the first character to use
* @param int $length Maximum number of characters used
* @param string $encoding The character encoding
* @return string The substring of $str
*/
public static function substr($str, $start, $length = null, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->substr($start, $length);
}
/**
* Returns the character at $index, with indexes starting at 0.
*
* @param string $str The string from which to get the char
* @param int $index Position of the character
* @param string $encoding The character encoding
* @return string The character at $index
*/
public static function at($str, $index, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->at($index);
}
/**
* Returns the first $n characters of the string.
*
* @param string $str The string from which to get the substring
* @param int $n Number of chars to retrieve from the start
* @param string $encoding The character encoding
* @return string The first $n characters
*/
public static function first($str, $n, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->first($n);
}
/**
* Returns the last $n characters of the string.
*
* @param string $str The string from which to get the substring
* @param int $n Number of chars to retrieve from the end
* @param string $encoding The character encoding
* @return string The last $n characters
*/
public static function last($str, $n, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->last($n);
}
/**
* Ensures that the string begins with $substring. If it doesn't, it's
* prepended.
*
* @param string $str The string to modify
* @param string $substring The substring to add if not present
* @param string $encoding The character encoding
* @return string The string prefixed by the $substring
*/
public static function ensureLeft($str, $substring, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->ensureLeft($substring);
}
/**
* Ensures that the string begins with $substring. If it doesn't, it's
* appended.
*
* @param string $str The string to modify
* @param string $substring The substring to add if not present
* @param string $encoding The character encoding
* @return string The string suffixed by the $substring
*/
public static function ensureRight($str, $substring, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->ensureRight($substring);
}
/**
* Returns a new string with the prefix $substring removed, if present.
*
* @param string $str String from which to remove the prefix
* @param string $substring The prefix to remove
* @param string $encoding The character encoding
* @return string The string without the prefix $substring
*/
public static function removeLeft($str, $substring, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->removeLeft($substring);
}
/**
* Returns a new string with the suffix $substring removed, if present.
*
* @param string $str String from which to remove the suffix
* @param string $substring The suffix to remove
* @param string $encoding The character encoding
* @return string The string without the suffix $substring
*/
public static function removeRight($str, $substring, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->removeRight($substring);
}
/**
* Returns true if the string contains a lower case char, false
* otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains a lower case character.
*/
public static function hasLowerCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->hasLowerCase();
}
/**
* Returns true if the string contains an upper case char, false
* otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains an upper case character.
*/
public static function hasUpperCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->hasUpperCase();
}
/**
* Returns true if the string contains only alphabetic chars, false
* otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only alphabetic chars
*/
public static function isAlpha($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isAlpha();
}
/**
* Returns true if the string contains only alphabetic and numeric chars,
* false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only alphanumeric chars
*/
public static function isAlphanumeric($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isAlphanumeric();
}
/**
* Returns true if the string contains only whitespace chars, false
* otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only whitespace characters
*/
public static function isBlank($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isBlank();
}
/**
* Returns true if the string is JSON, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str is JSON
*/
public static function isJson($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isJson();
}
/**
* Returns true if the string contains only lower case chars, false
* otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only lower case characters
*/
public static function isLowerCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isLowerCase();
}
/**
* Returns true if the string is serialized, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str is serialized
*/
public static function isSerialized($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isSerialized();
}
/**
* Returns true if the string contains only upper case chars, false
* otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only upper case characters
*/
public static function isUpperCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isUpperCase();
}
/**
* Returns true if the string contains only hexadecimal chars, false
* otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only hexadecimal characters
*/
public static function isHexadecimal($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isHexadecimal();
}
/**
* Returns the number of occurrences of $substring in the given string.
* By default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $str The string to search through
* @param string $substring The substring to search for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return int The number of $substring occurrences
*/
public static function countSubstr($str, $substring, $caseSensitive = true,
$encoding = null)
{
return Stringy::create($str, $encoding)
->countSubstr($substring, $caseSensitive);
}
/**
* Replaces all occurrences of $search in $str by $replacement.
*
* @param string $str The haystack to search through
* @param string $search The needle to search for
* @param string $replacement The string to replace with
* @param string $encoding The character encoding
* @return string The resulting string after the replacements
*/
public static function replace($str, $search, $replacement, $encoding = null)
{
return (string) Stringy::create($str, $encoding)
->replace($search, $replacement);
}
/**
* Replaces all occurrences of $pattern in $str by $replacement. An alias
* for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
* in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
* in the bundled version of Oniguruma in PHP 5.3.
*
* @param string $str The haystack to search through
* @param string $pattern The regular expression pattern
* @param string $replacement The string to replace with
* @param string $options Matching conditions to be used
* @param string $encoding The character encoding
* @return string The resulting string after the replacements
*/
public static function regexReplace($str, $pattern, $replacement,
$options = 'msr', $encoding = null)
{
return (string) Stringy::create($str, $encoding)
->regexReplace($pattern, $replacement, $options, $encoding);
}
/**
* Convert all applicable characters to HTML entities.
*
* @param string $str The string to encode.
* @param int|null $flags See http://php.net/manual/en/function.htmlentities.php
* @param string $encoding The character encoding
* @return Stringy Object with the resulting $str after being html encoded.
*/
public static function htmlEncode($str, $flags = ENT_COMPAT, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->htmlEncode($flags);
}
/**
* Convert all HTML entities to their applicable characters.
*
* @param string $str The string to decode.
* @param int|null $flags See http://php.net/manual/en/function.html-entity-decode.php
* @param string $encoding The character encoding
* @return Stringy Object with the resulting $str after being html decoded.
*/
public static function htmlDecode($str, $flags = ENT_COMPAT, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->htmlDecode($flags);
}
}

1558
src/Stringy.php Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,714 +0,0 @@
<?php
namespace Stringy;
class StaticStringy
{
/**
* Converts the first character of the supplied string to upper case.
*
* @param string $str String to modify
* @param string $encoding The character encoding
* @return string String with the first character being upper case
*/
public static function upperCaseFirst($str, $encoding = null)
{
return Stringy::create($str, $encoding)->upperCaseFirst()->str;
}
/**
* Converts the first character of the supplied string to lower case.
*
* @param string $str String to modify
* @param string $encoding The character encoding
* @return string String with the first character being lower case
*/
public static function lowerCaseFirst($str, $encoding = null)
{
return Stringy::create($str, $encoding)->lowerCaseFirst()->str;
}
/**
* Returns a camelCase version of the supplied string. Trims surrounding
* spaces, capitalizes letters following digits, spaces, dashes and
* underscores, and removes spaces, dashes, underscores.
*
* @param string $str String to convert to camelCase
* @param string $encoding The character encoding
* @return string String in camelCase
*/
public static function camelize($str, $encoding = null)
{
return Stringy::create($str, $encoding)->camelize()->str;
}
/**
* Returns an UpperCamelCase version of the supplied string. It trims
* surrounding spaces, capitalizes letters following digits, spaces, dashes
* and underscores, and removes spaces, dashes, underscores.
*
* @param string $str String to convert to UpperCamelCase
* @param string $encoding The character encoding
* @return string String in UpperCamelCase
*/
public static function upperCamelize($str, $encoding = null)
{
return Stringy::create($str, $encoding)->upperCamelize()->str;
}
/**
* Returns a lowercase and trimmed string separated by dashes. Dashes are
* inserted before uppercase characters (with the exception of the first
* character of the string), and in place of spaces as well as underscores.
*
* @param string $str String to convert
* @param string $encoding The character encoding
* @return string Dasherized string
*/
public static function dasherize($str, $encoding = null)
{
return Stringy::create($str, $encoding)->dasherize()->str;
}
/**
* Returns a lowercase and trimmed string separated by underscores.
* Underscores are inserted before uppercase characters (with the exception
* of the first character of the string), and in place of spaces as well as
* dashes.
*
* @param string $str String to convert
* @param string $encoding The character encoding
* @return string Underscored string
*/
public static function underscored($str, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->underscored()->str;
}
/**
* Returns a case swapped version of the string.
*
* @param string $str String to swap case
* @param string $encoding The character encoding
* @return string String with each character's case swapped
*/
public static function swapCase($str, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->swapCase()->str;
}
/**
* Returns a trimmed string with the first letter of each word capitalized.
* Ignores the case of other letters, allowing for the use of acronyms.
* Also accepts an array, $ignore, allowing you to list words not to be
* capitalized.
*
* @param string $str String to titleize
* @param string $encoding The character encoding
* @param array $ignore An array of words not to capitalize
* @return string Titleized string
*/
public static function titleize($str, $ignore = null, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->titleize($ignore)->str;
}
/**
* Capitalizes the first word of the string, replaces underscores with
* spaces, and strips '_id'.
*
* @param string $str String to humanize
* @param string $encoding The character encoding
* @return string A humanized string
*/
public static function humanize($str, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->humanize()->str;
}
/**
* Returns a string with smart quotes, ellipsis characters, and dashes from
* Windows-1252 (commonly used in Word documents) replaced by their ASCII
* equivalents.
*
* @param string $str String to remove special chars
* @param string $encoding The character encoding
* @return string String with those characters removed
*/
public static function tidy($str)
{
return $result = Stringy::create($str)->tidy()->str;
}
/**
* Trims the string and replaces consecutive whitespace characters with a
* single space. This includes tabs and newline characters, as well as
* multibyte whitespace such as the thin space and ideographic space.
*
* @param string $str The string to cleanup whitespace
* @return string The trimmed string with condensed whitespace
*/
public static function collapseWhitespace($str, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->collapseWhitespace()->str;
}
/**
* Returns an ASCII version of the string. A set of non-ASCII characters are
* replaced with their closest ASCII counterparts, and the rest are removed.
*
* @param string $str A string with non-ASCII characters
* @return string A string containing only ASCII characters
*/
public static function toAscii($str)
{
return $result = Stringy::create($str)->toAscii()->str;
}
/**
* Pads the string to a given length with $padStr. If length is less than
* or equal to the length of the string, no padding takes places. The default
* string used for padding is a space, and the default type (one of 'left',
* 'right', 'both') is 'right'. Throws an InvalidArgumentException if
* $padType isn't one of those 3 values.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $padType One of 'left', 'right', 'both'
* @param string $encoding The character encoding
* @return string The padded string
* @throws InvalidArgumentException If $padType isn't one of 'right',
* 'left' or 'both'
*/
public static function pad($str, $length, $padStr = ' ', $padType = 'right',
$encoding = null)
{
return $result = Stringy::create($str, $encoding)
->pad($length, $padStr, $padType)->str;
}
/**
* Returns a new string of a given length such that the beginning of the
* string is padded. Alias for pad() with a $padType of 'left'.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $encoding The character encoding
* @return string The padded string
*/
public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
{
return Stringy::create($str, $encoding)->padLeft($length, $padStr)->str;
}
/**
* Returns a new string of a given length such that the end of the string is
* padded. Alias for pad() with a $padType of 'right'.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $encoding The character encoding
* @return string The padded string
*/
public static function padRight($str, $length, $padStr = ' ', $encoding = null)
{
return Stringy::create($str, $encoding)->padRight($length, $padStr)->str;
}
/**
* Returns a new string of a given length such that both sides of the string
* string are padded. Alias for pad() with a $padType of 'both'.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $encoding The character encoding
* @return string The padded string
*/
public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
{
return Stringy::create($str, $encoding)->padBoth($length, $padStr)->str;
}
/**
* Returns true if the string begins with $substring, false otherwise.
* By default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $str String to check the start of
* @param string $substring The substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $str starts with $substring
*/
public static function startsWith($str, $substring, $caseSensitive = true,
$encoding = null)
{
return Stringy::create($str, $encoding)
->startsWith($substring, $caseSensitive);
}
/**
* Returns true if the string ends with $substring, false otherwise. By
* default, the comparison is case-sensitive, but can be made insensitive by
* setting $caseSensitive to false.
*
* @param string $str String to check the end of
* @param string $substring The substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $str ends with $substring
*/
public static function endsWith($str, $substring, $caseSensitive = true,
$encoding = null)
{
return Stringy::create($str, $encoding)
->endsWith($substring, $caseSensitive);
}
/**
* Converts each tab in the string to some number of spaces, as defined by
* $tabLength. By default, each tab is converted to 4 consecutive spaces.
*
* @param string $str String to convert tabs to spaces
* @param int $tabLength Number of spaces to replace each tab with
* @return string String with tabs switched to spaces
*/
public static function toSpaces($str, $tabLength = 4)
{
return Stringy::create($str)->toSpaces($tabLength)->str;
}
/**
* Converts each occurrence of some consecutive number of spaces, as defined
* by $tabLength, to a tab. By default, each 4 consecutive spaces are
* converted to a tab.
*
* @param string $str String to convert spaces to tabs
* @param int $tabLength Number of spaces to replace with a tab
* @return string String with spaces switched to tabs
*/
public static function toTabs($str, $tabLength = 4)
{
return Stringy::create($str)->toTabs($tabLength)->str;
}
/**
* Converts the string into an URL slug. This includes replacing non-ASCII
* characters with their closest ASCII equivalents, removing non-alphanumeric
* and non-ASCII characters, and replacing whitespace with dashes. The string
* is also converted to lowercase.
*
* @param string $str Text to transform into an URL slug
* @return string The corresponding URL slug
*/
public static function slugify($str)
{
return Stringy::create($str)->slugify()->str;
}
/**
* Returns true if the string contains $needle, false otherwise.
*
* @param string $haystack String being checked
* @param string $needle Substring to look for
* @param string $encoding The character encoding
* @return bool Whether or not $haystack contains $needle
*/
public static function contains($haystack, $needle, $encoding = null)
{
return Stringy::create($haystack, $encoding)->contains($needle);
}
/**
* Surrounds a string with the given substring.
*
* @param string $str The string to surround
* @param string $substring The substring to add to both sides
* @return string The string with the substring prepended and appended
*/
public static function surround($str, $substring)
{
return Stringy::create($str)->surround($substring)->str;
}
/**
* Inserts $substring into the string at the $index provided.
*
* @param string $str String to insert into
* @param string $substring String to be inserted
* @param int $index The index at which to insert the substring
* @param string $encoding The character encoding
* @return string The resulting string after the insertion
*/
public static function insert($str, $substring, $index, $encoding = null)
{
return Stringy::create($str, $encoding)->insert($substring, $index)->str;
}
/**
* Truncates the string to a given length. If $substring is provided, and
* truncating occurs, the string is further truncated so that the substring
* may be appended without exceeding the desired length.
*
* @param string $str String to truncate
* @param int $length Desired length of the truncated string
* @param string $substring The substring to append if it can fit
* @param string $encoding The character encoding
* @return string The resulting string after truncating
*/
public static function truncate($str, $length, $substring = '',
$encoding = null)
{
return Stringy::create($str, $encoding)
->truncate($length, $substring)->str;
}
/**
* Truncates the string to a given length, while ensuring that it does not
* chop words. If $substring is provided, and truncating occurs, the string
* is further truncated so that the substring may be appended without
* exceeding the desired length.
*
* @param string $str String to truncate
* @param int $length Desired length of the truncated string
* @param string $substring The substring to append if it can fit
* @param string $encoding The character encoding
* @return string The resulting string after truncating
*/
public static function safeTruncate($str, $length, $substring = '',
$encoding = null)
{
return Stringy::create($str, $encoding)
->safeTruncate($length, $substring)->str;
}
/**
* Returns a reversed string. A multibyte version of strrev().
*
* @param string $str String to reverse
* @param string $encoding The character encoding
* @return string The reversed string
*/
public static function reverse($str, $encoding = null)
{
return Stringy::create($str, $encoding)->reverse()->str;
}
/**
* A multibyte str_shuffle() function. It returns a string with its
* characters in random order.
*
* @param string $str String to shuffle
* @param string $encoding The character encoding
* @return string The shuffled string
*/
public static function shuffle($str, $encoding = null)
{
return Stringy::create($str, $encoding)->shuffle()->str;
}
/**
* Returns the trimmed string. An alias for PHP's trim() function.
*
* @param string $str String to trim
* @return string Trimmed $str
*/
public static function trim($str)
{
return trim($str);
}
/**
* Returns the longest common prefix between the string and $otherStr.
*
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
* @param string $encoding The character encoding
* @return string The longest common prefix
*/
public static function longestCommonPrefix($str, $otherStr, $encoding = null)
{
return Stringy::create($str, $encoding)
->longestCommonPrefix($otherStr)->str;
}
/**
* Returns the longest common suffix between the string and $otherStr.
*
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
* @param string $encoding The character encoding
* @return string The longest common suffix
*/
public static function longestCommonSuffix($str, $otherStr, $encoding = null)
{
return Stringy::create($str, $encoding)
->longestCommonSuffix($otherStr)->str;
}
/**
* Returns the longest common substring between the string and $otherStr.
* In the case of ties, it returns that which occurs first.
*
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
* @param string $encoding The character encoding
* @return string The longest common substring
*/
public static function longestCommonSubstring($str, $otherStr,
$encoding = null)
{
return Stringy::create($str, $encoding)
->longestCommonSubstring($otherStr)->str;
}
/**
* Returns the length of the string. An alias for PHP's mb_strlen() function.
*
* @param string $str The string to get the length of
* @param string $encoding The character encoding
* @return int The number of characters in $str given the encoding
*/
public static function length($str, $encoding = null)
{
return Stringy::create($str, $encoding)->length();
}
/**
* Returns the substring beginning at $start with the specified $length.
* It differs from the mb_substr() function in that providing a $length of
* null will return the rest of the string, rather than an empty string.
*
* @param string $str The string to get the length of
* @param int $start Position of the first character to use
* @param int $length Maximum number of characters used
* @param string $encoding The character encoding
* @return string The substring of $str
*/
public static function substr($str, $start, $length = null, $encoding = null)
{
return Stringy::create($str, $encoding)->substr($start, $length)->str;
}
/**
* Returns the character of the string at $index, with indexes starting at 0.
*
* @param string $str The string from which to get the char
* @param int $index Position of the character
* @param string $encoding The character encoding
* @return string The character at $index
*/
public static function at($str, $index, $encoding = null)
{
return Stringy::create($str, $encoding)->at($index)->str;
}
/**
* Returns the first $n characters of the string.
*
* @param string $str The string from which to get the substring
* @param int $n Number of chars to retrieve from the start
* @param string $encoding The character encoding
* @return string The first $n characters
*/
public static function first($str, $n, $encoding = null)
{
return Stringy::create($str, $encoding)->first($n)->str;
}
/**
* Returns the last $n characters of the string.
*
* @param string $str The string from which to get the substring
* @param int $n Number of chars to retrieve from the end
* @param string $encoding The character encoding
* @return string The last $n characters
*/
public static function last($str, $n, $encoding = null)
{
return Stringy::create($str, $encoding)->last($n)->str;
}
/**
* Ensures that the string begins with $substring. If it doesn't, it's
* prepended.
*
* @param string $str The string to modify
* @param string $substring The substring to add if not present
* @param string $encoding The character encoding
* @return string The string prefixed by the $substring
*/
public static function ensureLeft($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->ensureLeft($substring)->str;
}
/**
* Ensures that the string begins with $substring. If it doesn't, it's
* appended.
*
* @param string $str The string to modify
* @param string $substring The substring to add if not present
* @param string $encoding The character encoding
* @return string The string suffixed by the $substring
*/
public static function ensureRight($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->ensureRight($substring)->str;
}
/**
* Returns a new string with the prefix $substring removed, if it was present.
*
* @param string $str String from which to remove the prefix
* @param string $substring The prefix to remove
* @param string $encoding The character encoding
* @return string The string without the prefix $substring
*/
public static function removeLeft($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->removeLeft($substring)->str;
}
/**
* Returns a new string with the suffix $substring removed, if it was present.
*
* @param string $str String from which to remove the suffix
* @param string $substring The suffix to remove
* @param string $encoding The character encoding
* @return string The string without the suffix $substring
*/
public static function removeRight($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->removeRight($substring)->str;
}
/**
* Returns true if the string contains only alphabetic chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only alphabetic chars
*/
public static function isAlpha($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isAlpha();
}
/**
* Returns true if the string contains only alphabetic and numeric chars,
* false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only alphanumeric chars
*/
public static function isAlphanumeric($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isAlphanumeric();
}
/**
* Returns true if the string contains only whitespace chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only whitespace characters
*/
public static function isBlank($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isBlank();
}
/**
* Returns true if the string is JSON, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str is JSON
*/
public static function isJson($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isJson();
}
/**
* Returns true if the string contains only lower case chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only lower case characters
*/
public static function isLowerCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isLowerCase();
}
/**
* Returns true if the string is serialized, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str is serialized
*/
public static function isSerialized($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isSerialized();
}
/**
* Returns true if the string contains only upper case chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only upper case characters
*/
public static function isUpperCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isUpperCase();
}
/**
* Returns true if the string contains only hexadecimal chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only hexadecimal characters
*/
public static function isHexadecimal($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isHexadecimal();
}
/**
* Returns the number of occurrences of $substring in the given string.
* An alias for mb_substr_count()
*
* @param string $str The string to search through
* @param string $substring The substring to search for
* @param string $encoding The character encoding
* @return int The number of $substring occurrences
*/
public static function count($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->count($substring);
}
/**
* Returns a string with all occurrences of $search replaced with $replace.
*
* @param string $str The haystack to search through
* @param string $search The needle to search for
* @param string $replace The string to replace with
* @param string $encoding The character encoding
* @return string The resulting string after the replacements
*/
public static function replace($str, $search, $replace, $encoding = null)
{
return Stringy::create($str, $encoding)->replace($search, $replace)->str;
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

16
tests/CreateTest.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
require __DIR__ . '/../src/Create.php';
use function Stringy\create as s;
class CreateTestCase extends PHPUnit_Framework_TestCase
{
public function testCreate()
{
$stringy = s('foo bar', 'UTF-8');
$this->assertInstanceOf('Stringy\Stringy', $stringy);
$this->assertEquals('foo bar', (string) $stringy);
$this->assertEquals('UTF-8', $stringy->getEncoding());
}
}

View File

@@ -1,14 +1,44 @@
<?php
$base = realpath(dirname(__FILE__) . '/../..');
require("$base/src/Stringy/StaticStringy.php");
require __DIR__ . '/../src/StaticStringy.php';
use Stringy\StaticStringy as S;
class StaticStringyTestCase extends CommonTest
{
/**
* @dataProvider indexOfProvider()
*/
public function testIndexOf($expected, $str, $subStr, $offset = 0, $encoding = null)
{
$result = S::indexOf($str, $subStr, $offset, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForUpperCaseFirst
* @dataProvider indexOfLastProvider()
*/
public function testIndexOfLast($expected, $str, $subStr, $offset = 0, $encoding = null)
{
$result = S::indexOfLast($str, $subStr, $offset, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider charsProvider()
*/
public function testChars($expected, $str, $encoding = null)
{
$result = S::chars($str, $encoding);
$this->assertInternalType('array', $result);
foreach ($result as $char) {
$this->assertInternalType('string', $char);
}
$this->assertEquals($expected, $result);
}
/**
* @dataProvider upperCaseFirstProvider()
*/
public function testUpperCaseFirst($expected, $str, $encoding = null)
{
@@ -18,7 +48,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForLowerCaseFirst
* @dataProvider lowerCaseFirstProvider()
*/
public function testLowerCaseFirst($expected, $str, $encoding = null)
{
@@ -28,7 +58,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForCamelize
* @dataProvider camelizeProvider()
*/
public function testCamelize($expected, $str, $encoding = null)
{
@@ -38,7 +68,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForUpperCamelize
* @dataProvider upperCamelizeProvider()
*/
public function testUpperCamelize($expected, $str, $encoding = null)
{
@@ -48,7 +78,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForDasherize
* @dataProvider dasherizeProvider()
*/
public function testDasherize($expected, $str, $encoding = null)
{
@@ -58,7 +88,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForUnderscored
* @dataProvider underscoredProvider()
*/
public function testUnderscored($expected, $str, $encoding = null)
{
@@ -68,7 +98,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForSwapCase
* @dataProvider swapCaseProvider()
*/
public function testSwapCase($expected, $str, $encoding = null)
{
@@ -78,7 +108,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForTitleize
* @dataProvider titleizeProvider()
*/
public function testTitleize($expected, $str, $ignore = null,
$encoding = null)
@@ -89,7 +119,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForHumanize
* @dataProvider humanizeProvider()
*/
public function testHumanize($expected, $str, $encoding = null)
{
@@ -99,7 +129,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForTidy
* @dataProvider tidyProvider()
*/
public function testTidy($expected, $str)
{
@@ -109,7 +139,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForCollapseWhitespace
* @dataProvider collapseWhitespaceProvider()
*/
public function testCollapseWhitespace($expected, $str, $encoding = null)
{
@@ -119,17 +149,17 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForToAscii
* @dataProvider toAsciiProvider()
*/
public function testToAscii($expected, $str)
public function testToAscii($expected, $str, $removeUnsupported = true)
{
$result = S::toAscii($str);
$result = S::toAscii($str, $removeUnsupported);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForPad
* @dataProvider padProvider()
*/
public function testPad($expected, $str, $length, $padStr = ' ',
$padType = 'right', $encoding = null)
@@ -140,7 +170,15 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForPadLeft
* @expectedException \InvalidArgumentException
*/
public function testPadException()
{
$result = S::pad('string', 5, 'foo', 'bar');
}
/**
* @dataProvider padLeftProvider()
*/
public function testPadLeft($expected, $str, $length, $padStr = ' ',
$encoding = null)
@@ -151,7 +189,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForPadRight
* @dataProvider padRightProvider()
*/
public function testPadRight($expected, $str, $length, $padStr = ' ',
$encoding = null)
@@ -162,7 +200,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForPadBoth
* @dataProvider padBothProvider()
*/
public function testPadBoth($expected, $str, $length, $padStr = ' ',
$encoding = null)
@@ -173,10 +211,10 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForStartsWith
* @dataProvider startsWithProvider()
*/
public function testStartsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
$caseSensitive = true, $encoding = null)
{
$result = S::startsWith($str, $substring, $caseSensitive, $encoding);
$this->assertInternalType('boolean', $result);
@@ -184,10 +222,10 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForEndsWith
* @dataProvider endsWithProvider()
*/
public function testEndsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
$caseSensitive = true, $encoding = null)
{
$result = S::endsWith($str, $substring, $caseSensitive, $encoding);
$this->assertInternalType('boolean', $result);
@@ -195,7 +233,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForToSpaces
* @dataProvider toSpacesProvider()
*/
public function testToSpaces($expected, $str, $tabLength = 4)
{
@@ -205,7 +243,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForToTabs
* @dataProvider toTabsProvider()
*/
public function testToTabs($expected, $str, $tabLength = 4)
{
@@ -215,27 +253,80 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForSlugify
* @dataProvider toLowerCaseProvider()
*/
public function testSlugify($expected, $str)
public function testToLowerCase($expected, $str, $encoding = null)
{
$result = S::slugify($str);
$result = S::toLowerCase($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForContains
* @dataProvider toTitleCaseProvider()
*/
public function testContains($expected, $haystack, $needle, $encoding = null)
public function testToTitleCase($expected, $str, $encoding = null)
{
$result = S::contains($haystack, $needle, $encoding);
$result = S::toTitleCase($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider toUpperCaseProvider()
*/
public function testToUpperCase($expected, $str, $encoding = null)
{
$result = S::toUpperCase($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider slugifyProvider()
*/
public function testSlugify($expected, $str, $replacement = '-')
{
$result = S::slugify($str, $replacement);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider containsProvider()
*/
public function testContains($expected, $haystack, $needle,
$caseSensitive = true, $encoding = null)
{
$result = S::contains($haystack, $needle, $caseSensitive, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSurround
* @dataProvider containsAnyProvider()
*/
public function testcontainsAny($expected, $haystack, $needles,
$caseSensitive = true, $encoding = null)
{
$result = S::containsAny($haystack, $needles, $caseSensitive, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider containsAllProvider()
*/
public function testContainsAll($expected, $haystack, $needles,
$caseSensitive = true, $encoding = null)
{
$result = S::containsAll($haystack, $needles, $caseSensitive, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider surroundProvider()
*/
public function testSurround($expected, $str, $substring)
{
@@ -245,7 +336,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForInsert
* @dataProvider insertProvider()
*/
public function testInsert($expected, $str, $substring, $index,
$encoding = null)
@@ -256,7 +347,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForTruncate
* @dataProvider truncateProvider()
*/
public function testTruncate($expected, $str, $length, $substring = '',
$encoding = null)
@@ -267,7 +358,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForSafeTruncate
* @dataProvider safeTruncateProvider()
*/
public function testSafeTruncate($expected, $str, $length, $substring = '',
$encoding = null)
@@ -278,7 +369,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForReverse
* @dataProvider reverseProvider()
*/
public function testReverse($expected, $str, $encoding = null)
{
@@ -288,7 +379,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForShuffle
* @dataProvider shuffleProvider()
*/
public function testShuffle($str, $encoding = null)
{
@@ -309,17 +400,39 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForTrim
* @dataProvider trimProvider()
*/
public function testTrim($expected, $str)
public function testTrim($expected, $str, $chars = null, $encoding = null)
{
$result = S::trim($str);
$result = S::trim($str, $chars, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLongestCommonPrefix
* @dataProvider trimLeftProvider()
*/
public function testTrimLeft($expected, $str, $chars = null,
$encoding = null)
{
$result = S::trimLeft($str, $chars, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider trimRightProvider()
*/
public function testTrimRight($expected, $str, $chars = null,
$encoding = null)
{
$result = S::trimRight($str, $chars, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider longestCommonPrefixProvider()
*/
public function testLongestCommonPrefix($expected, $str, $otherStr,
$encoding = null)
@@ -330,7 +443,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForLongestCommonSuffix
* @dataProvider longestCommonSuffixProvider()
*/
public function testLongestCommonSuffix($expected, $str, $otherStr,
$encoding = null)
@@ -341,7 +454,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForLongestCommonSubstring
* @dataProvider longestCommonSubstringProvider()
*/
public function testLongestCommonSubstring($expected, $str, $otherStr,
$encoding = null)
@@ -352,7 +465,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForLength
* @dataProvider lengthProvider()
*/
public function testLength($expected, $str, $encoding = null)
{
@@ -362,7 +475,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForSubstr
* @dataProvider substrProvider()
*/
public function testSubstr($expected, $str, $start, $length = null,
$encoding = null)
@@ -373,7 +486,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForAt
* @dataProvider atProvider()
*/
public function testAt($expected, $str, $index, $encoding = null)
{
@@ -383,7 +496,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForFirst
* @dataProvider firstProvider()
*/
public function testFirst($expected, $str, $n, $encoding = null)
{
@@ -393,7 +506,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForLast
* @dataProvider lastProvider()
*/
public function testLast($expected, $str, $n, $encoding = null)
{
@@ -403,7 +516,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForEnsureLeft
* @dataProvider ensureLeftProvider()
*/
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
{
@@ -413,7 +526,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForEnsureRight
* @dataProvider ensureRightProvider()
*/
public function testEnsureRight($expected, $str, $substring, $encoding = null)
{
@@ -423,7 +536,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForRemoveLeft
* @dataProvider removeLeftProvider()
*/
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
{
@@ -433,7 +546,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForRemoveRight
* @dataProvider removeRightProvider()
*/
public function testRemoveRight($expected, $str, $substring, $encoding = null)
{
@@ -443,7 +556,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForIsAlpha
* @dataProvider isAlphaProvider()
*/
public function testIsAlpha($expected, $str, $encoding = null)
{
@@ -453,7 +566,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForIsAlphanumeric
* @dataProvider isAlphanumericProvider()
*/
public function testIsAlphanumeric($expected, $str, $encoding = null)
{
@@ -463,7 +576,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForIsBlank
* @dataProvider isBlankProvider()
*/
public function testIsBlank($expected, $str, $encoding = null)
{
@@ -471,9 +584,9 @@ class StaticStringyTestCase extends CommonTest
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsJson
* @dataProvider isJsonProvider()
*/
public function testIsJson($expected, $str, $encoding = null)
{
@@ -483,7 +596,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForIsLowerCase
* @dataProvider isLowerCaseProvider()
*/
public function testIsLowerCase($expected, $str, $encoding = null)
{
@@ -493,7 +606,17 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForIsSerialized
* @dataProvider hasLowerCaseProvider()
*/
public function testHasLowerCase($expected, $str, $encoding = null)
{
$result = S::hasLowerCase($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider isSerializedProvider()
*/
public function testIsSerialized($expected, $str, $encoding = null)
{
@@ -503,7 +626,7 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForIsUpperCase
* @dataProvider isUpperCaseProvider()
*/
public function testIsUpperCase($expected, $str, $encoding = null)
{
@@ -513,7 +636,17 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForIsHexadecimal
* @dataProvider hasUpperCaseProvider()
*/
public function testHasUpperCase($expected, $str, $encoding = null)
{
$result = S::hasUpperCase($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider isHexadecimalProvider()
*/
public function testIsHexadecimal($expected, $str, $encoding = null)
{
@@ -523,22 +656,54 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForCount
* @dataProvider countSubstrProvider()
*/
public function testCount($expected, $str, $substring, $encoding = null)
public function testCountSubstr($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
{
$result = S::count($str, $substring, $encoding);
$result = S::countSubstr($str, $substring, $caseSensitive, $encoding);
$this->assertInternalType('int', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForReplace
* @dataProvider replaceProvider()
*/
public function testReplace($expected, $str, $search, $replace,
public function testReplace($expected, $str, $search, $replacement,
$encoding = null)
{
$result = S::replace($str, $search, $replace, $encoding);
$result = S::replace($str, $search, $replacement, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider regexReplaceProvider()
*/
public function testRegexReplace($expected, $str, $pattern, $replacement,
$options = 'msr', $encoding = null)
{
$result = S::regexReplace($str, $pattern, $replacement, $options, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider htmlEncodeProvider()
*/
public function testHtmlEncode($expected, $str, $flags = ENT_COMPAT, $encoding = null)
{
$result = S::htmlEncode($str, $flags, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider htmlDecodeProvider()
*/
public function testHtmlDecode($expected, $str, $flags = ENT_COMPAT, $encoding = null)
{
$result = S::htmlDecode($str, $flags, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}

File diff suppressed because it is too large Load Diff