1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-30 08:20:12 +02:00

117 Commits

Author SHA1 Message Date
Daniel St. Jules
df24ab62d2 3.1.0 2017-06-11 21:10:27 -04:00
Daniel St. Jules
f9c2e0ae36 Remove ь/Ь from default mappping, add to bg-specific 2017-06-11 21:06:36 -04:00
Daniel St. Jules
0c0b352009 Update mapping of ЬЪ/ьъ from latin B/b to Y/y 2017-06-11 20:55:10 -04:00
Daniel St. Jules
f0a5c0ec80 Update table of contents 2017-06-11 20:43:36 -04:00
Daniel St. Jules
8ef4bddd15 Add support to slugify 2017-06-11 20:41:02 -04:00
Daniel St. Jules
d9cee68330 Merge pull request #164 from iipavlov/hotfix/cyrillic_ascii_transliteration 2017-05-10 10:41:34 -04:00
iipavlov
2838f3412a Added bg specific transliteration according BGN/PCGN (2013) 2017-05-05 18:20:56 +03:00
iipavlov
50f0cb6356 Moved the cyrillic letters ь and ъ from the latin b tto y. 2017-05-05 17:54:57 +03:00
Daniel St. Jules
d6dabeee67 Merge pull request #163 from emirb/patch-1
Add PHP 7.1 to Travis build
2017-04-22 00:37:48 -04:00
Emir Beganović
44f492c025 Add PHP 7.1 to Travis build 2017-04-14 17:46:53 +02:00
Daniel St. Jules
2288363663 3.0.1 2017-04-12 11:20:39 -04:00
Daniel St. Jules
f22117862b Use normal replacement for @ in slugify 2017-04-12 11:19:35 -04:00
Daniel St. Jules
eb13cc35d6 Don't replace @ in toAscii 2017-04-12 11:09:07 -04:00
Daniel St. Jules
9b6bddcb20 Fix list items 2017-04-09 21:57:32 -04:00
Daniel St. Jules
67f16c9423 Use HTML anchors in readme table 2017-04-09 21:56:46 -04:00
Daniel St. Jules
3708b6ba90 Update alignment in phpdocs 2017-03-13 23:10:57 -04:00
Daniel St. Jules
5c50a58bb8 Fix readme typo 2017-03-08 19:56:22 -05:00
Daniel St. Jules
8b59cd2233 3.0.0 2017-03-08 19:52:40 -05:00
Daniel St. Jules
63d81901ad Merge pull request #160 from danielstjules/3.x-dev
3.x
2017-03-08 19:44:04 -05:00
Daniel St. Jules
a2624d4969 Docs fixes 2017-03-08 19:23:05 -05:00
Daniel St. Jules
72102ae911 toAscii changes: rename locale to language, correct some transliterations 2017-03-08 19:16:09 -05:00
Daniel St. Jules
bf2a302aa1 Breaking change: Fix docs, force static methods to return strings
rather than Stringy instances
2017-03-06 15:11:14 -05:00
Alexey Shockov
5303203a72 Return real strings from StaticStringy 2017-03-06 14:00:09 -05:00
Daniel St. Jules
e01ba7f848 Use short array syntax 2017-03-06 13:43:47 -05:00
Daniel St. Jules
f9f3b7f2f2 Link to 2.x documentation 2017-03-06 13:15:11 -05:00
Daniel St. Jules
75b0cd2e0d Drop php 5.3 support 2017-03-06 13:14:15 -05:00
Daniel St. Jules
b0976c72b7 Fix #116 with breaking change: add locale parameter to toAscii
This fixes a previous regression in handling of some umlaut
characters, and commits a breaking change by adding the param
before $removeUnsupported
2017-03-06 13:01:09 -05:00
Daniel St. Jules
edbda419cb 2.4.0 2017-03-02 15:43:29 -05:00
Daniel St. Jules
e8afcffc7c Add ў and Ў to charsArray 2017-03-02 15:33:26 -05:00
Daniel St. Jules
986260239d Fix safeTruncate tests 2017-03-02 15:18:53 -05:00
forevermatt
841600867d Fix safeTruncate to handle strings without spaces and those where the first word is longer than the desired truncation length
Before this, calling safeTruncate with a string without spaces and a truncate length less than the string length would return an empty string.
2017-03-02 15:18:53 -05:00
Daniel St. Jules
845e8d693d Merge pull request #153 from vpassapera/feature/fix-phpdoc
Fixes phpdoc for objects extending stringy.
2017-03-01 00:33:19 -05:00
Daniel St. Jules
b35270671c Merge pull request #143 from fgambino/patch-1
Add full width character forms to ascii map
2017-02-28 23:54:27 -05:00
Daniel St. Jules
0aab4361bf Merge pull request #157 from OlivierBarbier/master
Reuse substr method
2017-02-28 23:46:09 -05:00
Daniel St. Jules
df777da6ac Add endsWithAny 2017-02-28 23:41:49 -05:00
Daniel St. Jules
ded4d41d44 Use table for linking to methods 2017-02-28 23:31:03 -05:00
Daniel St. Jules
569736327a Fix readme link 2017-02-28 23:24:33 -05:00
Daniel St. Jules
6f2e2851ac Add startsWithAny to readme 2017-02-28 23:23:43 -05:00
Daniel St. Jules
553bf37acb Merge pull request #151 from vikpe/master
Add documentation for stripWhitespace()
2017-02-28 23:16:59 -05:00
Olivier Barbier
916f79221e Reuse substr method 2017-02-16 12:13:56 +01:00
Victor Passapera
d71d4f0ad6 Fixes phpdoc for objects extending stringy. 2017-01-19 22:42:06 -08:00
Daniel St. Jules
de93eab01f Merge pull request #147 from mimmi20/master
New method request: startsWithAny()
2016-12-23 12:46:28 -08:00
Thomas Müller
9b6404d5c3 update array syntax 2016-12-23 21:28:39 +01:00
Viktor Persson
27a4f11f2e Added documentation for stripWhitespace(). 2016-12-22 10:37:27 +01:00
Daniel St. Jules
46de78bda9 Merge pull request #150 from vikpe/master
Add stripWhitespace
2016-12-22 01:03:59 -08:00
Daniel St. Jules
cb14fc2e6b Change private to protected to facilitate extending class 2016-12-21 23:28:07 -08:00
Daniel St. Jules
090a15f2fd Fix #142 - Fix and improve exception in supportsEncoding 2016-12-21 23:25:46 -08:00
Viktor Persson
d80347e72a Add stripWhitespace 2016-12-20 19:40:02 +01:00
Thomas Müller
0491ed8db8 add startsWithAny function 2016-10-31 19:54:19 +01:00
Frank Gambino
eb445b55ef Use utf-8 representation of the wide space char 2016-09-29 09:35:01 -04:00
Frank Gambino
91b153d30e Add wide non-breaking space 2016-09-29 09:26:35 -04:00
Frank Gambino
9ebdf88b71 Add full width character forms to ascii map
Reference: http://www.fileformat.info/info/unicode/block/halfwidth_and_fullwidth_forms/list.htm
2016-09-29 09:19:33 -04:00
Daniel St. Jules
0607751e17 Merge pull request #139 from tomeh/documentation-fixes
Updated documentation.
2016-06-14 13:50:55 -07:00
Tom Harris
6507f0ac44 Updated documentation.
Fixed presumed typos.
2016-06-14 21:13:57 +01:00
Daniel St. Jules
4e214a5195 2.3.2 2016-05-02 08:18:10 -07:00
Daniel St. Jules
fcc2969210 Merge pull request #137 from danielstjules/mbstring
Fix #134: Improve support without mbstring
2016-05-02 08:14:50 -07:00
Daniel St. Jules
30d1742082 Work on full support with mbstring module 2016-05-01 19:16:24 -07:00
Daniel St. Jules
bd90918858 2.3.1 2016-03-21 13:26:21 -07:00
Daniel St. Jules
a2d1ec535a Fix #129: Always use root namespace for mbstring functions 2016-03-21 13:22:27 -07:00
Daniel St. Jules
b953a85d2c Add capture group test to regexReplaceProvider 2016-03-19 17:57:04 -07:00
Daniel St. Jules
e07a07a076 Add capture group example to readme 2016-03-19 17:53:09 -07:00
Daniel St. Jules
c4d785d29f Fix changelog date 2016-03-19 17:34:20 -07:00
Daniel St. Jules
5554477962 2.3.0 2016-03-19 17:33:13 -07:00
Daniel St. Jules
87c1a47355 Support older verisons of composer 2016-03-19 17:21:08 -07:00
Daniel St. Jules
6d558974ba Merge pull request #120 from lucasmichot/feature/master/useless-else
Remove some useless else.
2016-02-10 13:20:49 -08:00
Daniel St. Jules
5a9b987f9a Merge pull request #124 from lucasmichot/feature/master/integrates-polyfill
Add polyfill
2016-02-09 16:59:23 -08:00
Lucas Michot
e3d397fa74 Add polyfill 2016-02-10 01:56:06 +01:00
Daniel St. Jules
ffc3fa5ff9 Merge pull request #112 from lucasmichot/feature/master/polyfill
WIP - Allows Stringy to work without ext-mbstring
2016-02-09 16:43:13 -08:00
Lucas Michot
c3f25577ed Avoid duplicate jobs 2016-02-10 01:38:59 +01:00
Lucas Michot
5a4629662c Add tests matrix 2016-02-10 01:28:12 +01:00
Lucas Michot
823e50a340 Allows Stringy to work without ext-mbstring 2016-01-08 18:35:53 +01:00
Daniel St. Jules
9d77c49fca Merge pull request #117 from lucasmichot/feature/master/phpunit-autoload
Let phpunit load autoload.php
2016-01-08 09:32:17 -08:00
Daniel St. Jules
a074d2a2ac Merge pull request #118 from lucasmichot/feature/master/import
Avoid FQN in code
2016-01-08 09:30:30 -08:00
Daniel St. Jules
b7cfa84219 Merge pull request #119 from lucasmichot/feature/master/export-ignore-editorconfig
Do not export .editorconfig
2016-01-08 09:25:15 -08:00
Lucas Michot
e3be6a56ea Composer install for travisCI tests. 2016-01-08 18:22:09 +01:00
Lucas Michot
bdbd4d7541 Let phpunit load autoload.php 2016-01-08 18:01:20 +01:00
Lucas Michot
8419369f3b Add some imports 2016-01-08 18:01:13 +01:00
Lucas Michot
a3dde38a2f Do not export .editorconfig 2016-01-08 18:00:43 +01:00
Lucas Michot
59ce593086 Remove some useless else. 2016-01-08 18:00:23 +01:00
Daniel St. Jules
54cf92c89c Merge pull request #115 from behzadsh/master
Add Persian characters in Stringy::charsArray()
2015-12-27 11:17:23 -05:00
Behzad Shabani
d320bf8704 Add Persian characters in Stringy::charsArray() 2015-12-27 16:03:33 +03:30
Daniel St. Jules
2de4c4aee3 Add badges 2015-12-26 08:13:47 -08:00
Daniel St. Jules
fc39b70953 2.2 2015-12-20 16:49:59 -08:00
Daniel St. Jules
9b61dcc771 Remove y char 2015-12-20 16:45:53 -08:00
Daniel St. Jules
b85a84d0b9 Fix #108 Improve char coverage 2015-12-20 16:44:46 -08:00
Daniel St. Jules
e5c3043d75 Style fixes 2015-12-20 13:22:02 -08:00
Daniel St. Jules
49a7405a2a Remove package annotation from #105 2015-12-20 13:19:29 -08:00
Daniel St. Jules
63109ce85c Add @method lines to StaticStringy 2015-12-20 13:08:08 -08:00
Daniel St. Jules
b223a2c320 Small fixes from #105 2015-12-20 12:08:36 -08:00
Daniel St. Jules
264eed18dd Merge pull request #111 from lucasmichot/feature/master/gitattributes
Add a basic .gitattributes file and avoid some exports
2015-12-20 14:51:11 -05:00
Daniel St. Jules
d990a41a1a Update isBase64 to return true if an empty string 2015-12-20 11:46:33 -08:00
Marcel Pociot
318db789b1 Added isBase64 method 2015-12-20 11:44:18 -08:00
Daniel St. Jules
f086c67ac7 Merge pull request #114 from danielstjules/reflection
Reflection
2015-12-20 14:37:45 -05:00
Daniel St. Jules
ee59d586f2 PHP 5.3 support with reflection 2015-12-20 11:35:48 -08:00
samuelwilliams
3c7bc662a6 :: is dynamically generated. 2015-12-20 17:01:52 +11:00
Daniel St. Jules
2072e667e9 Merge pull request #109 from lucasmichot/feature/master/editorconfig
Add a .editorconfig file
2015-12-08 13:29:02 -05:00
Lucas Michot
b65da66ae8 Add a .gitattributes file and avoid some exports 2015-12-06 14:20:38 +01:00
Lucas Michot
604ddc0528 Add a .editorconfig file 2015-12-06 14:11:58 +01:00
Daniel St. Jules
abd5505e98 Merge pull request #103 from danielkesselberg/master
Update for German umlaut transformation
2015-10-19 22:54:55 -07:00
Daniel Kesselberg
ee4b89c2f8 Update for German umlaut transformation (Ä => AE, Ö => UE, Ü => UE, ä => ae, ö => oe, ü => ue) 2015-09-24 10:47:35 +02:00
Daniel St. Jules
ac04be8416 Add note on PHP 7 compatibility 2015-09-09 22:17:45 -07:00
Daniel St. Jules
41336996a1 Fix ensureRight doc comment 2015-09-09 22:06:27 -07:00
Daniel St. Jules
a8793ff711 Merge pull request #100 from joshdifabio/patch-1
Fix copy/paste mistake in README
2015-09-09 22:05:46 -07:00
Daniel St. Jules
183e529e15 Merge pull request #102 from Big-Shark/newTravisInfrastructure
Changed travis infrastructure
2015-09-09 21:59:17 -07:00
Daniel St. Jules
4673754054 Fix: isJSON now returns false for empty strings 2015-09-09 21:24:11 -07:00
Big_Shark
83671e721a Changed travis infrastructure 2015-09-10 11:19:33 +07:00
Daniel St. Jules
c5f370c46d Merge pull request #101 from Big-Shark/php7
Added php7 for travis
2015-09-09 21:18:47 -07:00
Big_Shark
436d269622 Added php7 for travis 2015-09-10 11:16:21 +07:00
Josh Di Fabio
c09de057a1 Fix copy/paste mistake in README 2015-09-08 13:37:21 +01:00
Daniel St. Jules
efb10020f6 2.1 2015-09-02 23:50:48 -07:00
Daniel St. Jules
ae74e66369 Update readme notes on StaticStringy 2015-09-02 23:47:07 -07:00
Daniel St. Jules
f6475e1288 Stringy::create str defaults to empty string 2015-09-02 23:46:48 -07:00
Daniel St. Jules
ff36c79892 Add test for StaticStringy invocation 2015-09-02 23:37:17 -07:00
Daniel St. Jules
8f40b3495d String is now optional in Stringy constructor 2015-09-02 23:35:32 -07:00
Daniel St. Jules
e237f30d94 Bring back StaticStringy as a __callStatic wrapper 2015-09-02 00:12:25 -07:00
Daniel St. Jules
ad6d32080f Update titleize description in readme 2015-07-29 02:09:00 -07:00
Daniel St. Jules
7377aabf61 Fix titleize example 2015-07-29 02:05:16 -07:00
12 changed files with 2303 additions and 1469 deletions

16
.editorconfig Normal file
View File

@@ -0,0 +1,16 @@
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
[*.yml]
indent_style = space
indent_size = 2

8
.gitattributes vendored Normal file
View File

@@ -0,0 +1,8 @@
* text=auto
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
tests export-ignore

View File

@@ -1,7 +1,43 @@
language: php
php:
- 5.6
- 5.5
- 5.4
- 5.3
- hhvm
# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false
matrix:
fast_finish: true
include:
- php: 5.4
env: polyfill='true'
- php: 5.4
env: polyfill='false'
- php: 5.5
env: polyfill='true'
- php: 5.5
env: polyfill='false'
- php: 5.6
env: polyfill='true'
- php: 5.6
env: polyfill='false'
- php: 7.0
env: polyfill='true'
- php: 7.0
env: polyfill='false'
- php: 7.1
env: polyfill='true'
- php: 7.1
env: polyfill='false'
install: travis_retry composer install --no-interaction --prefer-source
before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source
- if [[ $polyfill = 'false' ]]; then travis_retry composer remove symfony/polyfill-mbstring; fi
script:
- vendor/bin/phpunit

View File

@@ -1,3 +1,57 @@
### 3.1.0 (2017-06-11)
* Add $language support to slugify
* Add bg specific transliteration
* ЬЪ/ьъ handling is now language-specific
### 3.0.1 (2017-04-12)
* Don't replace @ in toAscii
* Use normal replacement for @ in slugify, e.g. user@home => user-home
### 3.0.0 (2017-03-08)
* Breaking change: added $language parameter to toAscii, before
$removeUnsupported
* Breaking change: dropped PHP 5.3 support
* Breaking change: any StaticStringy methods that previously returned instances
of Stringy now return strings
### 2.4.0 (2017-03-02)
* Add startsWithAny
* Add endsWithAny
* Add stripWhitespace
* Fix error handling for unsupported encodings
* Change private methods to protected for extending class
* Fix safeTruncate for strings without spaces
* Additional char support in toAscii, e.g. full width chars and wide
non-breaking space
### 2.3.2 (2016-05-02)
* Improve support without mbstring
### 2.3.1 (2016-03-21)
* Always use root namespace for mbstring functions
### 2.3.0 (2016-03-19)
* Add Persian characters in Stringy::charsArray()
* Use symfony/polyfill-mbstring to avoid dependency on ext-mbstring
### 2.2.0 (2015-12-20)
* isJSON now returns false for empty strings
* Update for German umlaut transformation
* Use reflection to generate method list for StaticStringy
* Added isBase64 method
* Improved toAscii char coverage
### 2.1.0 (2015-09-02)
* Added simplified StaticStringy class
* str in Stringy::create and constructor is now optional
### 2.0.0 (2015-07-29)
* Removed StaticStringy class

333
README.md
View File

@@ -1,99 +1,152 @@
![Stringy](http://danielstjules.com/github/stringy-logo.png)
A PHP string manipulation library with multibyte support. Compatible with PHP
5.3+ and HHVM. Refer to the [1.x branch](https://github.com/danielstjules/Stringy/tree/1.x)
for older documentation.
5.4+, PHP 7+, and HHVM.
``` php
s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
```
Refer to the [1.x branch](https://github.com/danielstjules/Stringy/tree/1.x) or
[2.x branch](https://github.com/danielstjules/Stringy/tree/2.x) for older
documentation.
[![Build Status](https://api.travis-ci.org/danielstjules/Stringy.svg?branch=master)](https://travis-ci.org/danielstjules/Stringy)
[![Total Downloads](https://poser.pugx.org/danielstjules/stringy/downloads)](https://packagist.org/packages/danielstjules/stringy)
[![License](https://poser.pugx.org/danielstjules/stringy/license)](https://packagist.org/packages/danielstjules/stringy)
* [Why?](#why)
* [Installation](#installation)
* [OO and Chaining](#oo-and-chaining)
* [Implemented Interfaces](#implemented-interfaces)
* [PHP 5.6 Creation](#php-56-creation)
* [StaticStringy](#staticstringy)
* [Class methods](#class-methods)
* [create](#createmixed-str--encoding-)
* [Instance methods](#instance-methods)
* [append](#appendstring-string)
* [at](#atint-index)
* [between](#betweenstring-start-string-end--int-offset)
* [camelize](#camelize)
* [chars](#chars)
* [collapseWhitespace](#collapsewhitespace)
* [contains](#containsstring-needle--boolean-casesensitive--true-)
* [containsAll](#containsallarray-needles--boolean-casesensitive--true-)
* [containsAny](#containsanyarray-needles--boolean-casesensitive--true-)
* [countSubstr](#countsubstrstring-substring--boolean-casesensitive--true-)
* [dasherize](#dasherize)
* [delimit](#delimitint-delimiter)
* [endsWith](#endswithstring-substring--boolean-casesensitive--true-)
* [ensureLeft](#ensureleftstring-substring)
* [ensureRight](#ensurerightstring-substring)
* [first](#firstint-n)
* [getEncoding](#getencoding)
* [hasLowerCase](#haslowercase)
* [hasUpperCase](#hasuppercase)
* [htmlDecode](#htmldecode)
* [htmlEncode](#htmlencode)
* [humanize](#humanize)
* [indexOf](#indexofstring-needle--offset--0-)
* [indexOfLast](#indexoflaststring-needle--offset--0-)
* [insert](#insertint-index-string-substring)
* [isAlpha](#isalpha)
* [isAlphanumeric](#isalphanumeric)
* [isBlank](#isblank)
* [isHexadecimal](#ishexadecimal)
* [isJson](#isjson)
* [isLowerCase](#islowercase)
* [isSerialized](#isserialized)
* [isUpperCase](#isuppercase)
* [last](#last)
* [length](#length)
* [lines](#lines)
* [longestCommonPrefix](#longestcommonprefixstring-otherstr)
* [longestCommonSuffix](#longestcommonsuffixstring-otherstr)
* [longestCommonSubstring](#longestcommonsubstringstring-otherstr)
* [lowerCaseFirst](#lowercasefirst)
* [pad](#padint-length--string-padstr-----string-padtype--right-)
* [padBoth](#padbothint-length--string-padstr----)
* [padLeft](#padleftint-length--string-padstr----)
* [padRight](#padrightint-length--string-padstr----)
* [prepend](#prependstring-string)
* [regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr)
* [removeLeft](#removeleftstring-substring)
* [removeRight](#removerightstring-substring)
* [repeat](#repeatmultiplier)
* [replace](#replacestring-search-string-replacement)
* [reverse](#reverse)
* [safeTruncate](#safetruncateint-length--string-substring---)
* [shuffle](#shuffle)
* [slugify](#slugify-string-replacement----)
* [startsWith](#startswithstring-substring--boolean-casesensitive--true-)
* [slice](#sliceint-start--int-end-)
* [split](#splitstring-pattern--int-limit-)
* [substr](#substrint-start--int-length-)
* [surround](#surroundstring-substring)
* [swapCase](#swapcase)
* [tidy](#tidy)
* [titleize](#titleize-array-ignore)
* [toAscii](#toascii)
* [toBoolean](#toboolean)
* [toLowerCase](#tolowercase)
* [toSpaces](#tospaces-tablength--4-)
* [toTabs](#totabs-tablength--4-)
* [toTitleCase](#totitlecase)
* [toUpperCase](#touppercase)
* [trim](#trim-string-chars)
* [trimLeft](#trimleft-string-chars)
* [trimRight](#trimright-string-chars)
* [truncate](#truncateint-length--string-substring---)
* [underscored](#underscored)
* [upperCamelize](#uppercamelize)
* [upperCaseFirst](#uppercasefirst)
<table>
<tr>
<td><a href="#appendstring-string">append</a></td>
<td><a href="#atint-index">at</a></td>
<td><a href="#betweenstring-start-string-end--int-offset">between</a></td>
<td><a href="#camelize">camelize</a></td>
</tr>
<tr>
<td><a href="#chars">chars</a></td>
<td><a href="#collapsewhitespace">collapseWhitespace</a></td>
<td><a href="#containsstring-needle--boolean-casesensitive--true-">contains</a></td>
<td><a href="#containsallarray-needles--boolean-casesensitive--true-">containsAll</a></td>
</tr>
<tr>
<td><a href="#containsanyarray-needles--boolean-casesensitive--true-">containsAny</a></td>
<td><a href="#countsubstrstring-substring--boolean-casesensitive--true-">countSubstr</a></td>
<td><a href="#dasherize">dasherize</a></td>
<td><a href="#delimitint-delimiter">delimit</a></td>
</tr>
<tr>
<td><a href="#endswithstring-substring--boolean-casesensitive--true-">endsWith</a></td>
<td><a href="#endswithanystring-substrings--boolean-casesensitive--true-">endsWithAny</a></td>
<td><a href="#ensureleftstring-substring">ensureLeft</a></td>
<td><a href="#ensurerightstring-substring">ensureRight</a></td>
</tr>
<tr>
<td><a href="#firstint-n">first</a></td>
<td><a href="#getencoding">getEncoding</a></td>
<td><a href="#haslowercase">hasLowerCase</a></td>
<td><a href="#hasuppercase">hasUpperCase</a></td>
</tr>
<tr>
<td><a href="#htmldecode">htmlDecode</a></td>
<td><a href="#htmlencode">htmlEncode</a></td>
<td><a href="#humanize">humanize</a></td>
<td><a href="#indexofstring-needle--offset--0-">indexOf</a></td>
</tr>
<tr>
<td><a href="#indexoflaststring-needle--offset--0-">indexOfLast</a></td>
<td><a href="#insertint-index-string-substring">insert</a></td>
<td><a href="#isalpha">isAlpha</a></td>
<td><a href="#isalphanumeric">isAlphanumeric</a></td>
</tr>
<tr>
<td><a href="#isbase64">isBase64</a></td>
<td><a href="#isblank">isBlank</a></td>
<td><a href="#ishexadecimal">isHexadecimal</a></td>
<td><a href="#isjson">isJson</a></td>
</tr>
<tr>
<td><a href="#islowercase">isLowerCase</a></td>
<td><a href="#isserialized">isSerialized</a></td>
<td><a href="#isuppercase">isUpperCase</a></td>
<td><a href="#lastint-n">last</a></td>
</tr>
<tr>
<td><a href="#length">length</a></td>
<td><a href="#lines">lines</a></td>
<td><a href="#longestcommonprefixstring-otherstr">longestCommonPrefix</a></td>
<td><a href="#longestcommonsuffixstring-otherstr">longestCommonSuffix</a></td>
</tr>
<tr>
<td><a href="#longestcommonsubstringstring-otherstr">longestCommonSubstring</a></td>
<td><a href="#lowercasefirst">lowerCaseFirst</a></td>
<td><a href="#padint-length--string-padstr-----string-padtype--right-">pad</a></td>
<td><a href="#padbothint-length--string-padstr----">padBoth</a></td>
</tr>
<tr>
<td><a href="#padleftint-length--string-padstr----">padLeft</a></td>
<td><a href="#padrightint-length--string-padstr----">padRight</a></td>
<td><a href="#prependstring-string">prepend</a></td>
<td><a href="#regexreplacestring-pattern-string-replacement--string-options--msr">regexReplace</a></td>
</tr>
<tr>
<td><a href="#removeleftstring-substring">removeLeft</a></td>
<td><a href="#removerightstring-substring">removeRight</a></td>
<td><a href="#repeatint-multiplier">repeat</a></td>
<td><a href="#replacestring-search-string-replacement">replace</a></td>
</tr>
<tr>
<td><a href="#reverse">reverse</a></td>
<td><a href="#safetruncateint-length--string-substring---">safeTruncate</a></td>
<td><a href="#shuffle">shuffle</a></td>
<td><a href="#slugify-string-replacement-----string-language--en">slugify</a></td>
</tr>
<tr>
<td><a href="#sliceint-start--int-end-">slice</a></td>
<td><a href="#splitstring-pattern--int-limit-">split</a></td>
<td><a href="#startswithstring-substring--boolean-casesensitive--true-">startsWith</a></td>
<td><a href="#startswithanystring-substrings--boolean-casesensitive--true-">startsWithAny</a></td>
</tr>
<tr>
<td><a href="#stripwhitespace">stripWhitespace</a></td>
<td><a href="#substrint-start--int-length-">substr</a></td>
<td><a href="#surroundstring-substring">surround</a></td>
<td><a href="#swapcase">swapCase</a></td>
</tr>
<tr>
<td><a href="#tidy">tidy</a></td>
<td><a href="#titleize-array-ignore">titleize</a></td>
<td><a href="#toascii-string-language--en--bool-removeunsupported--true-">toAscii</a></td>
<td><a href="#toboolean">toBoolean</a></td>
</tr>
<tr>
<td><a href="#tolowercase">toLowerCase</a></td>
<td><a href="#tospaces-tablength--4-">toSpaces</a></td>
<td><a href="#totabs-tablength--4-">toTabs</a></td>
<td><a href="#totitlecase">toTitleCase</a></td>
</tr>
<tr>
<td><a href="#touppercase">toUpperCase</a></td>
<td><a href="#trim-string-chars">trim</a></td>
<td><a href="#trimleft-string-chars">trimLeft</a></td>
<td><a href="#trimright-string-chars">trimRight</a></td>
</tr>
<tr>
<td><a href="#truncateint-length--string-substring---">truncate</a></td>
<td><a href="#underscored">underscored</a></td>
<td><a href="#uppercamelize">upperCamelize</a></td>
<td><a href="#uppercasefirst">upperCaseFirst</a></td>
</tr>
</table>
* [Extensions](#extensions)
* [Tests](#tests)
* [License](#license)
@@ -127,7 +180,7 @@ in your composer.json file:
```json
"require": {
"danielstjules/stringy": "~2.0"
"danielstjules/stringy": "~3.1.0"
}
```
@@ -150,11 +203,13 @@ And in either case, I'd suggest using an alias.
use Stringy\Stringy as S;
```
Please note that Stringy relies on the `mbstring` PHP module for its underlying
multibyte support. This is a non-default, but very common module. For example,
with debian and ubuntu, it's included in libapache2-mod-php5, php5-cli, and
php5-fpm. For OSX users, it's a default for any version of PHP installed with
homebrew. If compiling PHP from scratch, it can be included with the
Please note that Stringy relies on the `mbstring` module for its underlying
multibyte support. If the module is not found, Stringy will use
[symfony/polyfill-mbstring](https://github.com/symfony/polyfill-mbstring).
ex-mbstring is a non-default, but very common module. For example, with debian
and ubuntu, it's included in libapache2-mod-php5, php5-cli, and php5-fpm. For
OSX users, it's a default for any version of PHP installed with homebrew.
If compiling PHP from scratch, it can be included with the
`--enable-mbstring` flag.
## OO and Chaining
@@ -223,6 +278,21 @@ use function Stringy\create as s;
s('fòô bàř')->collapseWhitespace()->swapCase();
```
## StaticStringy
All methods listed under "Instance methods" are available as part of a static
wrapper. For StaticStringy methods, the optional encoding is expected to be the
last argument. The return value is not cast, and may thus be of type Stringy,
integer, boolean, etc.
```php
use Stringy\StaticStringy as S;
// Translates to Stringy::create('fòôbàř')->slice(0, 3);
// Returns a Stringy object with the string "fòô"
S::slice('fòôbàř', 0, 3);
```
## Class methods
##### create(mixed $str [, $encoding ])
@@ -234,7 +304,7 @@ 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àř'
$stringy = S::create('fòôbàř'); // 'fòôbàř'
```
## Instance Methods
@@ -367,7 +437,17 @@ default, the comparison is case-sensitive, but can be made insensitive by
setting $caseSensitive to false.
```php
s('fòôbàř')->endsWith('bàř', true); // true
s('fòôbàř')->endsWith('bàř'); // true
```
##### endsWithAny(string[] $substrings [, boolean $caseSensitive = true ])
Returns true if the string ends with any of $substrings, false otherwise.
By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php
s('fòôbàř')->endsWithAny(['bàř', 'baz']); // true
```
##### ensureLeft(string $substring)
@@ -380,7 +460,7 @@ s('foobar')->ensureLeft('http://'); // 'http://foobar'
##### ensureRight(string $substring)
Ensures that the string begins with $substring. If it doesn't, it's appended.
Ensures that the string ends with $substring. If it doesn't, it's appended.
```php
s('foobar')->ensureRight('.com'); // 'foobar.com'
@@ -399,7 +479,7 @@ s('fòôbàř')->first(3); // 'fòô'
Returns the encoding used by the Stringy object.
```php
s('fòôbàř', 'UTF-8')->getEncoding(); // 'UTF-8'
s('fòôbàř')->getEncoding(); // 'UTF-8'
```
##### hasLowerCase()
@@ -493,6 +573,14 @@ otherwise.
s('دانيال1')->isAlphanumeric(); // true
```
##### isBase64()
Returns true if the string is base64 encoded, false otherwise.
```php
s('Zm9vYmFy')->isBase64(); // true
```
##### isBlank()
Returns true if the string contains only whitespace chars, false otherwise.
@@ -511,7 +599,9 @@ s('A102F')->isHexadecimal(); // true
##### isJson()
Returns true if the string is JSON, false otherwise.
Returns true if the string is JSON, false otherwise. Unlike json_decode
in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers,
in that an empty string is not considered valid JSON.
```php
s('{"foo":"bar"}')->isJson(); // true
@@ -655,7 +745,8 @@ to a lack of support in the bundled version of Oniguruma in PHP < 5.6,
and current versions of HHVM (3.8 and below).
```php
s('fòô ')->regexReplace('f[òô]+\s', 'bàř', 'msr'); // 'bàř'
s('fòô ')->regexReplace('f[òô]+\s', 'bàř'); // 'bàř'
s('fò')->regexReplace('(ò)', '\\1ô'); // 'fòô'
```
##### removeLeft(string $substring)
@@ -719,28 +810,19 @@ random order.
s('fòôbàř')->shuffle(); // 'àôřbòf'
```
##### slugify([, string $replacement = '-' ])
##### slugify([, string $replacement = '-' [, string $language = 'en']])
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.
is also converted to lowercase. The language of the source string can
also be supplied for language-specific transliteration.
```php
s('Using strings like fòô bàř')->slugify(); // 'using-strings-like-foo-bar'
```
##### startsWith(string $substring [, boolean $caseSensitive = true ])
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.
```php
s('FÒÔbàřbaz')->startsWith('fòôbàř', false); // true
```
##### slice(int $start [, int $end ])
Returns the substring beginning at $start, and up to, but not including
@@ -762,6 +844,36 @@ results.
s('foo,bar,baz')->split(',', 2); // ['foo', 'bar']
```
##### startsWith(string $substring [, boolean $caseSensitive = true ])
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.
```php
s('FÒÔbàřbaz')->startsWith('fòôbàř', false); // true
```
##### startsWithAny(string[] $substrings [, boolean $caseSensitive = true ])
Returns true if the string begins with any of $substrings, false
otherwise. By default the comparison is case-sensitive, but can be made
insensitive by setting $caseSensitive to false.
```php
s('FÒÔbàřbaz')->startsWithAny(['fòô', 'bàř'], false); // true
```
##### stripWhitespace()
Strip all whitespace characters. This includes tabs and newline
characters, as well as multibyte whitespace such as the thin space
and ideographic space.
```php
s(' Ο συγγραφέας ')->stripWhitespace(); // 'Οσυγγραφέας'
```
##### substr(int $start [, int $length ])
Returns the substring beginning at $start with the specified $length.
@@ -800,23 +912,28 @@ s('“I see…”')->tidy(); // '"I see..."'
##### titleize([, array $ignore])
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.
Also accepts an array, $ignore, allowing you to list words not to be
capitalized.
```php
$ignore = ['at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the'];
s('If you optimize everything, you will always be unhappy.')->titleize($ignore);
// 'I Like to Watch television'
s('i like to watch television')->titleize($ignore);
// 'I Like to Watch Television'
```
##### toAscii()
##### toAscii([, string $language = 'en' [, bool $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
unless instructed otherwise.
by default. The language or locale of the source string can be supplied
for language-specific transliteration in any of the following formats:
en, en_GB, or en-GB. For example, passing "de" results in "äöü" mapping
to "aeoeue" rather than "aou" as in other languages.
```php
s('fòôbàř')->toAscii(); // 'foobar'
s('äöü')->toAscii(); // 'aou'
s('äöü')->toAscii('de'); // 'aeoeue'
```
##### toBoolean()

View File

@@ -15,8 +15,8 @@
}
],
"require": {
"php": ">=5.3.0",
"ext-mbstring": "*"
"php": ">=5.4.0",
"symfony/polyfill-mbstring": "~1.1"
},
"require-dev": {
"phpunit/phpunit": "~4.0"

View File

@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit processIsolation="false"
<phpunit bootstrap="vendor/autoload.php"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Stringy">
<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>

161
src/StaticStringy.php Normal file
View File

@@ -0,0 +1,161 @@
<?php
namespace Stringy;
use BadMethodCallException;
use ReflectionClass;
use ReflectionMethod;
/**
* Class StaticStringy
*
* @method static string append(string $str, string $stringAppend, string $encoding = null)
* @method static string at(string $str, int $index, string $encoding = null)
* @method static string between(string $str, string $start, string $end, int $offset = 0, string $encoding = null)
* @method static string camelize(string $str, string $encoding = null)
* @method static string chars(string $str, string $encoding = null)
* @method static string collapseWhitespace(string $str, string $encoding = null)
* @method static bool contains(string $str, string $needle, bool $caseSensitive = true, string $encoding = null)
* @method static bool containsAll(string $str, string[] $needle, bool $caseSensitive = true, string $encoding = null)
* @method static bool containsAny(string $str, string[] $needle, bool $caseSensitive = true, string $encoding = null)
* @method static int count(string $str, string $encoding = null)
* @method static int countSubstr(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
* @method static string dasherize(string $str, string $encoding = null)
* @method static string delimit(string $str, string $delimiter, string $encoding = null)
* @method static bool endsWith(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
* @method static bool endsWithAny(string $str, string[] $substrings, bool $caseSensitive = true, string $encoding = null)
* @method static string ensureLeft(string $str, string $substring, string $encoding = null)
* @method static string ensureRight(string $str, string $substring, string $encoding = null)
* @method static string first(string $str, int $n, string $encoding = null)
* @method static bool hasLowerCase(string $str, string $encoding = null)
* @method static bool hasUpperCase(string $str, string $encoding = null)
* @method static string htmlDecode(string $str, int $flags = ENT_COMPAT, string $encoding = null)
* @method static string htmlEncode(string $str, int $flags = ENT_COMPAT, string $encoding = null)
* @method static string humanize(string $str, string $encoding = null)
* @method static int indexOf(string $str, string $needle, int $offset = 0, string $encoding = null)
* @method static int indexOfLast(string $str, string $needle, int $offset = 0, string $encoding = null)
* @method static string insert(string $str, string $substring, int $index = 0, string $encoding = null)
* @method static bool isAlpha(string $str, string $encoding = null)
* @method static bool isAlphanumeric(string $str, string $encoding = null)
* @method static bool isBase64(string $str, string $encoding = null)
* @method static bool isBlank(string $str, string $encoding = null)
* @method static bool isHexadecimal(string $str, string $encoding = null)
* @method static bool isJson(string $str, string $encoding = null)
* @method static bool isLowerCase(string $str, string $encoding = null)
* @method static bool isSerialized(string $str, string $encoding = null)
* @method static bool isUpperCase(string $str, string $encoding = null)
* @method static string last(string $str, string $encoding = null)
* @method static int length(string $str, string $encoding = null)
* @method static string[] lines(string $str, string $encoding = null)
* @method static string longestCommonPrefix(string $str, string $otherStr, string $encoding = null)
* @method static string longestCommonSuffix(string $str, string $otherStr, string $encoding = null)
* @method static string longestCommonSubstring(string $str, string $otherStr, string $encoding = null)
* @method static string lowerCaseFirst(string $str, string $encoding = null)
* @method static string pad(string $str, int $length, string $padStr = ' ', string $padType = 'right', string $encoding = null)
* @method static string padBoth(string $str, int $length, string $padStr = ' ', string $encoding = null)
* @method static string padLeft(string $str, int $length, string $padStr = ' ', string $encoding = null)
* @method static string padRight(string $str, int $length, string $padStr = ' ', string $encoding = null)
* @method static string prepend(string $str, string $string, string $encoding = null)
* @method static string regexReplace(string $str, string $pattern, string $replacement, string $options = 'msr', string $encoding = null)
* @method static string removeLeft(string $str, string $substring, string $encoding = null)
* @method static string removeRight(string $str, string $substring, string $encoding = null)
* @method static string repeat(string $str, int $multiplier, string $encoding = null)
* @method static string replace(string $str, string $search, string $replacement, string $encoding = null)
* @method static string reverse(string $str, string $encoding = null)
* @method static string safeTruncate(string $str, int $length, string $substring = '', string $encoding = null)
* @method static string shuffle(string $str, string $encoding = null)
* @method static string slugify(string $str, string $replacement = '-', string $encoding = null)
* @method static string slice(string $str, int $start, int $end = null, string $encoding = null)
* @method static string split(string $str, string $pattern, int $limit = null, string $encoding = null)
* @method static bool startsWith(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
* @method static bool startsWithAny(string $str, string[] $substrings, bool $caseSensitive = true, string $encoding = null)
* @method static string stripWhitespace(string $str, string $encoding = null)
* @method static string substr(string $str, int $start, int $length = null, string $encoding = null)
* @method static string surround(string $str, string $substring, string $encoding = null)
* @method static string swapCase(string $str, string $encoding = null)
* @method static string tidy(string $str, string $encoding = null)
* @method static string titleize(string $str, string $encoding = null)
* @method static string toAscii(string $str, string $language = 'en', bool $removeUnsupported = true, string $encoding = null)
* @method static bool toBoolean(string $str, string $encoding = null)
* @method static string toLowerCase(string $str, string $encoding = null)
* @method static string toSpaces(string $str, int $tabLength = 4, string $encoding = null)
* @method static string toTabs(string $str, int $tabLength = 4, string $encoding = null)
* @method static string toTitleCase(string $str, string $encoding = null)
* @method static string toUpperCase(string $str, string $encoding = null)
* @method static string trim(string $str, string $chars = null, string $encoding = null)
* @method static string trimLeft(string $str, string $chars = null, string $encoding = null)
* @method static string trimRight(string $str, string $chars = null, string $encoding = null)
* @method static string truncate(string $str, int $length, string $substring = '', string $encoding = null)
* @method static string underscored(string $str, string $encoding = null)
* @method static string upperCamelize(string $str, string $encoding = null)
* @method static string upperCaseFirst(string $str, string $encoding = null)
*/
class StaticStringy
{
/**
* A mapping of method names to the numbers of arguments it accepts. Each
* should be two more than the equivalent Stringy method. Necessary as
* static methods place the optional $encoding as the last parameter.
*
* @var string[]
*/
protected static $methodArgs = null;
/**
* Creates an instance of Stringy and invokes the given method with the
* rest of the passed arguments. The optional encoding is expected to be
* the last argument. For example, the following:
* StaticStringy::slice('fòôbàř', 0, 3, 'UTF-8'); translates to
* Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3);
* The result is not cast, so the return value may be of type Stringy,
* integer, boolean, etc.
*
* @param string $name
* @param mixed[] $arguments
*
* @return Stringy
*
* @throws \BadMethodCallException
*/
public static function __callStatic($name, $arguments)
{
if (!static::$methodArgs) {
$stringyClass = new ReflectionClass('Stringy\Stringy');
$methods = $stringyClass->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
$params = $method->getNumberOfParameters() + 2;
static::$methodArgs[$method->name] = $params;
}
}
if (!isset(static::$methodArgs[$name])) {
throw new BadMethodCallException($name . ' is not a valid method');
}
$numArgs = count($arguments);
$str = ($numArgs) ? $arguments[0] : '';
if ($numArgs === static::$methodArgs[$name]) {
$args = array_slice($arguments, 1, -1);
$encoding = $arguments[$numArgs - 1];
} else {
$args = array_slice($arguments, 1);
$encoding = null;
}
$stringy = Stringy::create($str, $encoding);
$result = call_user_func_array([$stringy, $name], $args);
$cast = function($val) {
if (is_object($val) && $val instanceof Stringy) {
return (string) $val;
} else {
return $val;
}
};
return is_array($result) ? array_map($cast, $result) : $cast($result);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,5 @@
<?php
require __DIR__ . '/../src/Create.php';
use function Stringy\create as s;
class CreateTestCase extends PHPUnit_Framework_TestCase

View File

@@ -0,0 +1,73 @@
<?php
use Stringy\StaticStringy as S;
class StaticStringyTestCase extends PHPUnit_Framework_TestCase
{
/**
* @expectedException BadMethodCallException
*/
public function testBadMethodCall()
{
$result = S::invalidMethod('foo');
}
public function testEmptyArgsInvocation()
{
$result = S::toLowerCase();
$this->assertEquals('', $result);
}
public function testInvocation()
{
$result = S::toLowerCase('FOOBAR');
$this->assertEquals('foobar', $result);
$this->assertInternalType('string', $result);
}
public function testPartialArgsInvocation()
{
$result = S::slice('foobar', 0, 3);
$this->assertEquals('foo', $result);
$this->assertInternalType('string', $result);
}
public function testFullArgsInvocation()
{
$result = S::slice('fòôbàř', 0, 3, 'UTF-8');
$this->assertEquals('fòô', $result);
$this->assertInternalType('string', $result);
}
public function testArrayReturnValue()
{
$result = S::lines("a\nb");
$this->assertEquals(['a', 'b'], $result);
$this->assertInternalType('array', $result);
foreach ($result as $val) {
$this->assertInternalType('string', $val);
}
}
/**
* Use reflection to ensure that all argument numbers are correct. Each
* static method should accept 2 more arguments than their Stringy
* equivalent.
*/
public function testArgumentNumbers()
{
$staticStringyClass = new ReflectionClass('Stringy\StaticStringy');
$stringyClass = new ReflectionClass('Stringy\Stringy');
// getStaticPropertyValue can't access protected properties
$properties = $staticStringyClass->getStaticProperties();
foreach ($properties['methodArgs'] as $method => $expected) {
$num = $stringyClass->getMethod($method)
->getNumberOfParameters() + 2;
$this->assertEquals($expected, $num,
'Invalid num args for ' . $method);
}
}
}

File diff suppressed because it is too large Load Diff