{"id":36980962,"url":"https://github.com/moccalotto/stringy","last_synced_at":"2026-01-13T22:50:44.503Z","repository":{"id":57018712,"uuid":"80917405","full_name":"moccalotto/stringy","owner":"moccalotto","description":"Easy, powerful and fluent string handling","archived":false,"fork":false,"pushed_at":"2017-12-10T13:39:43.000Z","size":133,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-01T14:45:09.617Z","etag":null,"topics":["declarative","fluent","strings","text","text-formatting","utf-8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/moccalotto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-04T12:16:34.000Z","updated_at":"2017-11-26T09:02:10.000Z","dependencies_parsed_at":"2022-08-22T12:00:25.079Z","dependency_job_id":null,"html_url":"https://github.com/moccalotto/stringy","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/moccalotto/stringy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moccalotto%2Fstringy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moccalotto%2Fstringy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moccalotto%2Fstringy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moccalotto%2Fstringy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moccalotto","download_url":"https://codeload.github.com/moccalotto/stringy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moccalotto%2Fstringy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28402159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["declarative","fluent","strings","text","text-formatting","utf-8"],"created_at":"2026-01-13T22:50:44.369Z","updated_at":"2026-01-13T22:50:44.487Z","avatar_url":"https://github.com/moccalotto.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Stringy\n\n[![Build Status](https://travis-ci.org/moccalotto/stringy.svg)](https://travis-ci.org/moccalotto/stringy)\n\nEasy, powerful and fluent string handling\n\n## Installation\n\nTo add this package as a local, per-project dependency to your project, simply add a dependency on\n `moccalotto/stringy` to your project's `composer.json` file.\n\nThis can be done via composer:\n\n```bash\ncomposer require moccalotto/stringy\n```\n\n## Documentation\n\nThe `Stringy` object is immutable. This means that all operations that return a *new* `Stringy` instance\nThis means that you will not have to `clone` the object if you need to do two different,\nbranching operations on a given string.\n\nOn the other hand, it is quite likely that you cannot identity-compare two stringy objects because they\nare often very short-lived. In cases where you need to check if two stringy objects contain the same\nstring, we suggest using the `is()` method. See the example below:\n\n```php\n$s0 = str('Foo');\n\n$s1 = $s0-\u003elower(),\n$s2 = $s0-\u003elower();\n\nif ($s0 === $s1) {\n    echo 'this code will not be executed';\n}\n\nif ($s1 === $s2) {\n    echo 'this code will not be executed';\n}\n\nif ($s1-\u003eis($s2)) {\n    echo 'this will work just fine';\n}\n\nif (strval($s1) === strval($s2)) {\n    echo 'this will also work';\n}\n```\n\n\n### Constructors\n\nNormal constructor.\n\n```php\n/**\n * Constructor.\n *\n * @param string      $string          the string contents of the Stringy object\n * @param string|null $currentEncoding the current encoding of $string\n */\npublic function __construct(string $string = '', string $currentEncoding = null)\n```\n\nStatic factory.\n\n```php\n/**\n * Factory.\n *\n * @param Stringy|string $string   The string to be Stringyfied.\n *                                 If $string is a (descendant of) Stringy, it will\n *                                 be cloned and converted to using $encoding\n * @param string|null    $encoding The encoding of the $string\n *\n * @return Stringy\n */\npublic static function create($string = '', string $encoding = null)\n```\n\nHelper factory function.\n\n```php\n/**\n * Stringify a string\n *\n * @param Stringy|string $string   The string to be Stringyfied.\n *                                 If $string is a (descendant of) Stringy, it will\n *                                 be cloned and converted to using $encoding.\n * @param string|null    $encoding The encoding of the $string\n *\n * @return Stringy\n */\nfunction str($string = '', string $encoding = null) : Stringy\n```\n\n\nCreate a random string.\n\n```php\n/**\n * Factory for a random string of a given length.\n *\n * @return Stringy\n */\npublic static function random($length)\n```\n\n\n### Get the content string.\n\n```php\n/**\n * Get the content string of this object encoded as $encoding.\n *\n * @param string|null $encodedAs The encoding to get the string as. NULL = mb_internal_encoding\n *\n * @return string\n */\npublic function string($encodedAs = null) : string\n```\n\n### String comparison\n\n```php\n/**\n * Compare this string to another.\n *\n * @param Stringy|string $string\n *\n * @return bool only true if the two strings are equal using strict (===) comparison.\n */\npublic function is($string) : bool\n```\n\n```php\n/**\n * Does the string contain $needle.\n *\n * @param Stringy|string $needle\n * @param int            $index\n *\n * @return bool\n */\npublic function contains($needle, int $index = 0) : bool\n```\n\n```php\n/**\n * Does the string start with $needle ?\n *\n * @param Stringy|string $needle.\n *\n * @return bool\n */\npublic function startsWith($needle) : bool\n```\n\n```php\n/**\n * Does the string end with $needle ?\n *\n * @param Stringy|string $needle.\n *\n * @return bool\n */\npublic function endsWith($needle) : bool\n```\n\n\n### Length (characters)\n\n```php\n/**\n * Get the length (in characters) of the content string.\n *\n * @return int\n */\npublic function length() : int\n```\n\n### Size (bytes)\n\n```php\n/**\n * Get the size (in bytes) of the content string.\n *\n * @return int\n */\npublic function size() : int\n```\n\n\n### Position of substring\n\n```php\n/**\n * Find a the position of the first character of $needle within this string.\n *\n * @param Stringy|string $needle The string to search for\n * @param int            $index  Which occurrance of the string to get the position of.\n *                               0 means the first match, 1 means the second match, etc.\n *                               -1 means the last match, -2 means the penultimate match, etc\n *\n * @return int|null The position of the first character of the $needle found.\n *                  NULL if $needle with the given $index could not be found\n *                  NOTE: that this behavior deviates from strpos in that strpos returns FALSE\n *                  in case $needle was not found.\n */\npublic function positionOf($needle, int $index = 0)\n```\n\n### Getting words and characters from the string.\n\nYou can get individual characters via the php array accessor language construct like so:\n\n```php\n// Accessing individual characters:\n$str = str('foo bar baz');\n\n$str[0]-\u003estring() === 'f'; // true\n$str[1]-\u003estring() === 'o'; // true\n$str[2]-\u003estring() === 'o'; // true\n\n$str[-3]-\u003estring() === 'b' // true\n$str[-2]-\u003estring() === 'a' // true\n$str[-1]-\u003estring() === 'z' // true\n\n$x = $str[30]; // OutOfRangeException\n```\n\n```php\n/**\n * Convert the content string into an array of words.\n *\n * Note that this method will not correctly split kanji, thai, braille, and\n * other scripts where words are not necessarily clearly bounded.\n *\n * @return Stringy[]\n */\npublic function words() : array\n```\n\n```php\n/**\n * Get an array of characters in the content string.\n *\n * @return Stringy[]\n */\npublic function characters() : array\n```\n\n### Map/transform the string\n\n```php\n/**\n * Transform the string.\n *\n * @param callable $callable a function with the signature (Stringy $string) : Stringy|string\n *\n * @return Stringy\n */\npublic function transform(callable $callable)\n```\n\nExample:\n\n```php\n$str = str('foo bar baz');\n\n$formatted = $str-\u003etransform(function (Stringy $stringy) {\n    // the output of str_rot13() is a string\n    // but it will automatically be converted to a\n    // Stringy instance using the default character set.\n    // If you return your own Stringy object, it will not be converted in any way.\n    return str_rot13($stringy-\u003easciiSafe());\n});\n\nprint $formatted-\u003estring();\n```\n\n### Fetch a substring\n\n```\n/**\n * Get a substring.\n *\n * @see http://php.net/manual/function.mb-substr.php\n *      for details about the $start and $length paramteers\n *\n * @param int      $start  The offset of the substring.\n *                         If negative, it counts backwards\n *                         from the end of the content string.\n * @param int|null $length The length of the substring to extract.\n *                         If negative, it counts backwards from\n *                         the end of the content string.\n *                         If NULL, the entire string after $start\n *                         is extracted.\n *\n * @return Stringy\n */\npublic function substring(int $start, int $length = null)\n```\n\n\n### Fetch segments of a string based on searches.\n\nFetch the part of the string that comes after a given search term.\n\n```php\n/**\n * Get the part of the string that comes after $needle.\n *\n * @example: str('foo bar baz')-\u003eafter('foo ') == 'bar baz'\n *\n * @param Stringy|string $needle\n * @param int            $index  Which occurrance of the string to search for:\n *                               0 means the first match, 1 means the second match, etc.\n *                               -1 means the last match, -2 means the penultimate match, etc\n *\n * @return Stringy a clone of $this with a content string containing the\n *                 part that comes after $needle. If $needle is not\n *                 found, an empty Stringy is returned\n */\npublic function after($needle, int $index = 0)\n```\n\nFetch the part of the string that comes before a given search term.\n\n```php\n/**\n * Get the part of the string before the first character of $needle.\n *\n * @example: str('foo bar baz')-\u003ebefore('foo ') == 'bar baz'\n *\n * @param Stringy|string $needle\n * @param int            $index  Which occurrance of the string to search for:\n *                               0 means the first match, 1 means the second match, etc.\n *                               -1 means the last match, -2 means the penultimate match, etc\n *\n * @return Stringy a clone of $this with a content string containing the\n *                 part that comes after $needle. If $needle is not\n *                 found, an empty Stringy is returned\n */\npublic function before($needle, int $index = 0)\n```\n\nFetch the part of the string that resides between two search terms.\n\n```php\n/**\n * Return the text that comes after $start and before $stop.\n *\n * @param Stringy|string $start\n * @param Stringy|string $stop\n * @param int            $pairIndex search for the nth pair and\n *                                  find what lies between those strings\n *\n * @return Stringy the string between $start and $stop\n */\npublic function between($start, $stop, int $pairIndex = 0)\n```\n\n### Remove parts of the string based on search terms.\n\nRemove the part of the string that comes after the given search term.\n\n```php\n/**\n * Remove the substring that comes after the nth $needle.\n *\n * @param Stringy|string $needle\n * @param int            $index\n *\n * @return Stringy\n */\npublic function removeAfter($needle, int $index = 0)\n```\n\nRemove the part of the string that comes before the given search term.\n\n```php\n/**\n * Remove the substring that comes before the nth $needle.\n *\n * @param Stringy|string $needle\n * @param int            $index\n *\n * @return Stringy\n */\npublic function removeBefore($needle, int $index = 0)\n```\n\n### Repetition\n\n\nRepeating a string\n\n```php\n/**\n * Repeat this string a number of times.\n *\n * @param int $times\n *\n * @return Stringy a string repeated $times times\n */\npublic function repeat(int $times)\n```\n\n```php\n/**\n * Remove repititions of substrings.\n *\n * If a substring is repeated 2 or more times in a row within the\n * content string,reduce it to being there only once.\n *\n * str('hello    world')-\u003eunrepeat(' ') would be turned into 'hello world'\n * str('foo    bar    baz')-\u003eunrepeat(' ') would be turned into 'foo bar baz'\n *\n * @param Stringy|string $substring\n *\n * @return Stringy\n */\npublic function unrepeat($substring)\n```\n\n### Add padding\n\n```php\n/**\n * Append padding to the right hand side of the string.\n *\n * @param int            $totalLengthOfResult the total length of the result string\n * @param Stringy|string $padding             the padding character to use\n *\n * @return Stringy\n */\npublic function rightPadded(int $totalLengthOfResult, $padding = ' ')\n```\n\n```php\n/**\n * Prepend padding to the left hand side of the string.\n *\n * @param int            $totalLengthOfResult the total length of the result string\n * @param Stringy|string $padding             the padding character to use\n *\n * @return Stringy\n */\npublic function leftPadded(int $totalLengthOfResult, $padding = ' ')\n```\n\n```php\n/**\n * Add padding to both sides of the content string such that it becomes centered.\n *\n * @param int            $totalLengthOfResult the total length of the result string\n * @param Stringy|string $padding             the padding character to use\n * @param string         $tieBreak            Can either be \"left\" or \"right\".\n *                                            In case the content string cannot be\n *                                            centered, should the content string\n *                                            be to the left of center or the right of\n *                                            center.\n *\n * @return Stringy\n */\npublic function centered(int $totalLengthOfResult, $padding = ' ', $tieBreak = 'left')\n```\n\n### Remove Padding\n\n```php\n/**\n * Remove all instances of $needle from the beginning of the content string.\n *\n * @param Stringy|string $needle the substring to remove from the\n *                               beginning of the content string\n *\n * @return Stringy\n */\npublic function leftTrim($needle)\n```\n\n```php\n/**\n * Remove all instances of $needle from the end of the content string.\n *\n * @param Stringy|string $needle the substring to remove from the\n *                               end of the content string\n *\n * @return Stringy\n */\npublic function rightTrim($needle)\n```\n\n```php\n/**\n * Remove all occurances of $strings from the beginning of the content string.\n *\n * @param array $strings An array of strings or Stringy objects.\n *\n * @return Stringy\n */\npublic function leftTrimAll(array $strings)\n```\n\n```php\n/**\n * Remove all occurances of $strings from the end of the content string.\n *\n * @param array $strings An array of strings or Stringy objects.\n *\n * @return Stringy\n */\npublic function rightTrimAll(array $strings)\n```\n\n### Appending and prepending\n\n```php\n/**\n * Append a string to $this.\n *\n * @param Stringy|string $other\n *\n * @return Stringy a clone of $this where contents of $other is prepended\n */\npublic function append($other)\n```\n\n```php\n/**\n * Prepend a string to $this.\n *\n * @param Stringy|string $other\n *\n * @return Stringy a clone of $this where contents of $other is prepended\n */\npublic function prepend($other)\n```\n\n```php\n/**\n * Surround the content string with two other strings.\n *\n * Essentially the same as calling prepend and append in one single operation.\n *\n * @param Stringy|string      $left  the string to be prepended to the content string\n * @param Stringy|string|null $right The string to be appended to the content string.\n *                                   if NULL, the $left string will be used.\n *\n * @return Stringy\n */\npublic function surroundWith($left, $right = null)\n```\n\n### Adjust Casing\n\n```php\n/**\n * Convert the content string to uppercase.\n *\n * @return Stringy\n */\npublic function upper()\n```\n\n```php\n/**\n * Convert the content string to lowercase.\n *\n * @return Stringy\n */\npublic function lower()\n```\n\n```php\n/**\n * Turn the first letter of every word uppercase.\n *\n * Does not interfere with the casing of the rest of the letters.\n * Words are defined as strings separated by a word-boundary (such as white space, dashes, dots, etc.)\n *\n * @return Stringy\n */\npublic function ucwords()\n```\n\n```php\n/**\n * Turn the first letter of every word lowercase.\n *\n * Does not interfere with the casing of the rest of the letters.\n * Words are defined as strings separated by a word-boundary (such as white space, dashes, dots, etc.)\n *\n * @return Stringy\n */\npublic function lcwords()\n```\n\n```php\n/**\n * Turn first letter uppercased.\n *\n * Do not change the casing of the rest of the letters.\n */\npublic function ucfirst()\n```\n\n```php\n/**\n * Turn first letter lowercased.\n *\n * Do not change the casing of the rest of the letters.\n */\npublic function lcfirst()\n```\n\n### Exploding and imploding\n\n```php\n/**\n * Split the string into segments.\n *\n * @see http://php.net/manual/function.explode.php\n *\n * @param Stringy|string $pattern\n * @param int            $limit\n *\n * @return Stringy[] array of strings as Stringy instances\n */\npublic function explode($pattern, int $limit = PHP_INT_MAX) : array\n```\n\n```php\n/**\n * Use the content string as glue to implode an array of strings.\n *\n * For instance: str(' + ')-\u003eglue(['this', 'that']) would yield the result \"this + that\".\n *\n * @see http://php.net/manual/function.implode.php\n *\n * @param array $strings An array of strings or Stringy objects that will be glued together.\n *\n * @return Stringy\n */\npublic function glue(array $strings)\n```\n\n### Replace and remove\n\n```php\n/**\n * Replace all occurrences of the $search string with the $replacement string.\n *\n * @see http://php.net/manual/function.str-replace.php\n *\n * @param Stringy|string $search\n * @param Stringy|string $replace\n *\n * @return Stringy\n */\npublic function replace($search, $replace)\n```\n\n```php\n/**\n * Perform to =\u003e from translation.\n *\n * @see http://php.net/manual/function.strtr.php\n *\n * @param array $replacePairs an array in the form array('from' =\u003e 'to', ...).\n *\n * @return Stringy a Stringy where all the occurrences\n *                 of the array keys have been replaced by the corresponding values.\n *                 The longest keys will be tried first.\n *                 Once a substring has been replaced,\n *                 its new value will not be searched again.\n */\npublic function replaceMany(array $replacePairs)\n```\n\n```php\n/**\n * Remove a substring. (I.e. replace $search with an empty string).\n *\n * @param Stringy|string $search the substring to be removed\n *\n * @return Stringy\n */\npublic function remove($search)\n```\n\n```php\n**\n * Remove a number of substrings.\n *\n * @param array $searches an array of strings (or Stringy objects)\n *                        to be removed\n *\n * @return Stringy\n */\npublic function removeMany(array $searches)\n```\n\n### Escaping\n\n```php\n/**\n * Ensure that all characters are ASCII.\n *\n * Convert non-ascii characters to ASCII if possible (i.e. 'ü' is converted to 'u' and 'æ' to 'ae').\n * Remove any characters that cannot be converted (i.e. most characters that are not based on the latin script).\n *\n * @return Stringy\n */\npublic function asciiSafe()\n```\n\n```php\n/**\n * Convert all non-ASCII  characters into html entities.\n *\n * @see http://php.net/manual/function.htmlentities.php\n *\n * @param int $flags See php documentation for htmlentities\n *\n * @return Stringy\n */\npublic function entityEncoded(int $flags = ENT_QUOTES | ENT_HTML5)\n```\n\n```php\n/**\n * Escape this string for use as html text.\n *\n * @see http://php.net/manual/function.htmlspecialchars.php\n *\n * @param int $flags See php documentation for htmlspecialchars\n *\n * @return Stringy\n */\npublic function escapeForHtml(int $flags = ENT_QUOTES | ENT_HTML5)\n```\n\n```php\n/**\n * Escape a string so it can be used in a regular expression.\n *\n * @param Stringy|string $delimiter the delimiter used to start and\n *                                  terminate the regular expression.\n *                                  Usually the forward slash will be\n *                                  used to encluse regular expression.\n *\n * @return Stringy|string\n */\npublic function escapeForRegex($delimiter)\n```\n\n\n### Formatting\n\n```php\n/**\n * Include the content string in another, using sprintf syntax.\n *\n * @see http://php.net/manual/function.sprintf.php\n *\n * @param Stringy|string $string      The sprintf-template string to use.\n *                                    Must include at least one \"%s\"\n * @param array          $extraParams extra params to use in the sprintf operation\n *\n * @return Stringy\n */\npublic function includeIn($string, array $extraParams = [])\n\n// example:\n\nstr('bar')-\u003eincludeIn('foo %s baz')-\u003eis('foo bar baz'); // true\n\nstr('bar')-\u003eincludeIn('foo %s %s', ['baz'])-\u003eis('foo bar baz'); // true\n```\n\n```php\n/**\n * Use the content string as a sprintf-template string.\n *\n * @see http://php.net/manual/function.vsprintf.php\n *\n * @param array $args an array of args to use á la vsprintf\n *\n * @return Stringy\n */\npublic function format(array $args)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoccalotto%2Fstringy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoccalotto%2Fstringy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoccalotto%2Fstringy/lists"}