{"id":19324946,"url":"https://github.com/spatie/string","last_synced_at":"2026-04-17T07:02:04.247Z","repository":{"id":33275358,"uuid":"36919962","full_name":"spatie/string","owner":"spatie","description":"String handling evolved","archived":false,"fork":false,"pushed_at":"2023-04-06T05:39:19.000Z","size":118,"stargazers_count":560,"open_issues_count":0,"forks_count":24,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-05-29T09:56:51.146Z","etag":null,"topics":["php","string"],"latest_commit_sha":null,"homepage":"https://murze.be/2015/06/common-string-functions/","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/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2015-06-05T07:55:45.000Z","updated_at":"2025-04-15T06:21:23.000Z","dependencies_parsed_at":"2024-11-17T04:02:49.886Z","dependency_job_id":null,"html_url":"https://github.com/spatie/string","commit_stats":{"total_commits":103,"total_committers":12,"mean_commits":8.583333333333334,"dds":0.4368932038834952,"last_synced_commit":"9782525f46475b5ba0736de5ca04c15655d06728"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/spatie/string","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fstring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fstring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fstring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fstring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/string/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fstring/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31918838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T18:22:33.417Z","status":"online","status_checked_at":"2026-04-17T02:00:06.879Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["php","string"],"created_at":"2024-11-10T02:07:42.901Z","updated_at":"2026-04-17T07:02:04.220Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":[],"sub_categories":[],"readme":"# String handling evolved\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/string.svg?style=flat-square)](https://packagist.org/packages/spatie/string)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Build Status](https://img.shields.io/travis/spatie/string/master.svg?style=flat-square)](https://travis-ci.org/spatie/string)\n[![Quality Score](https://img.shields.io/scrutinizer/g/spatie/string.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/string)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/string.svg?style=flat-square)](https://packagist.org/packages/spatie/string)\n\nThis package provides a handy way to work with strings in php.\n\nSpatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/string.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/string)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Install\n\nYou can install this package via composer:\n\n``` bash\ncomposer require spatie/string\n```\n\n## Usage\n\nFirst you must wrap a native string in a String-object. This can be done with the `string`-function.\n```php\nstring('myFirstString');\n```\n\nFrom then on you can chain methods like there's no tomorrow:\n\n```php\necho string('StartMiddleEnd')-\u003ebetween('Start', 'End')-\u003etoUpper(); // outputs \"MIDDLE\"\n```\n\nOf course you can keep concatenate the output with the `.`-operator we all know and love.\n\n```php \necho 'stuck in the ' . string('StartMiddleEnd')-\u003ebetween('Start', 'End')-\u003etoLower() . ' with you';\n```\n\nYou can also use offsets to manipulate a string.\n\n```php\necho string('hello')[1]-\u003etoUpper(); //outputs \"E\"\n\n$string = string('gray');\n$string[2] = 'e';\necho $string-\u003etoUpper(); //outputs \"GREY\"\n```\n\n## Provided methods\n\n### between\n```php\n/**\n * Get the string between the given start and end.\n *\n * @param $start\n * @param $end\n * @return \\Spatie\\String\\String\n */\npublic function between($start, $end)\n```\n\nExample:\n```php\nstring('StartMiddleEnd')-\u003ebetween('Start', 'End')-\u003etoUpper(); // MIDDLE\n```\n\n### toUpper\n```php\n/**\n * Convert the string to uppercase.\n *\n * @return \\Spatie\\String\\String\n */\npublic function toUpper()\n```\n\nExample:\n```php\nstring('string')-\u003etoUpper(); // STRING\n```\n\n### toLower\n```php\n/**\n * Convert the string to lowercase.\n *\n * @return \\Spatie\\String\\String\n */\npublic function toLower()\n```\n\nExample:\n```php\nstring('STRING')-\u003etoLower(); // string\n```\n\n### tease\n```php\n/**\n * Shortens a string in a pretty way. It will clean it by trimming\n * it, remove all double spaces and html. If the string is then still\n * longer than the specified $length it will be shortened. The end\n * of the string is always a full word concatinated with the\n * specified moreTextIndicator.\n *\n * @param int $length\n * @param string $moreTextIndicator\n * @return \\Spatie\\String\\String\n */\npublic function tease($length = 200, $moreTextIndicator = '...')\n```\n\nExample:\n```php\n$longText = 'Now that there is the Tec-9, a crappy spray gun from South Miami. This gun is advertised as the most popular gun in American crime. Do you believe that shit? It actually says that in the little book that comes with it: the most popular gun in American crime.'\nstring($longText)-\u003etease(10); // Now that...\n```\n\n### replaceFirst\n```php\n/**\n * Replace the first occurrence of a string.\n *\n * @param $search\n * @param $replace\n * @return \\Spatie\\String\\String\n */\npublic function replaceFirst($search, $replace)\n```\n\nExample:\n```php\n$sentence = 'A good thing is not a good thing.';\nstring($sentence)-\u003ereplaceFirst('good', 'bad'); // A bad thing is not a good thing.\n```\n\n### replaceLast\n```php\n/**\n * Replace the last occurrence of a string.\n *\n * @param $search\n * @param $replace\n * @return \\Spatie\\String\\String\n */\npublic function replaceLast($search, $replace)\n```\n\nExample:\n```php\n$sentence = 'A good thing is not a good thing.';\nstring($sentence)-\u003ereplaceLast('good', 'bad'); // A good thing is not a bad thing.\n```\n\n### prefix\n```php\n/**\n * Prefix a string.\n *\n * @param $string\n * @return \\Spatie\\String\\String\n */\npublic function prefix($string)\n```\n\nExample:\n```php\nstring('world')-\u003eprefix('hello '); //hello world\n```\n\n### suffix\n```php\n/**\n * Suffix a string.\n *\n * @param $string\n * @return \\Spatie\\String\\String\n */\npublic function suffix($string)\n```\n\nExample:\n```php\nstring('hello')-\u003esuffix(' world'); // hello world\n```\n\n### concat\nThis is identical to the `suffix`-function.\n\n### possessive\n```php\n/**\n * Get the possessive version of a string.\n *\n * @return \\Spatie\\String\\String\n */\npublic function possessive()\n```\n\nExample:\n```php\nstring('Bob')-\u003epossessive(); // Bob's\nstring('Charles')-\u003epossessive(); // Charles'\n```\n\n### segment\n```php\n/**\n * Get a segment from a string based on a delimiter.\n * Returns an empty string when the offset doesn't exist.\n * Use a negative index to start counting from the last element.\n * \n * @param string $delimiter\n * @param int $index\n * \n * @return \\Spatie\\String\\String\n */\npublic function segment($delimiter, $index)\n```\n\nRelated methods:\n```php\n/**\n * Get the first segment from a string based on a delimiter.\n * \n * @param string $delimiter\n * \n * @return \\Spatie\\String\\String\n */\npublic function firstSegment($delimiter)\n\n/**\n * Get the last segment from a string based on a delimiter.\n * \n * @param string $delimiter\n * \n * @return \\Spatie\\String\\String\n */\npublic function lastSegment($delimiter)\n```\n\nExample:\n```php\nstring('foo/bar/baz')-\u003esegment('/', 0); // foo\nstring('foo/bar/baz')-\u003esegment('/', 1); // bar\nstring('foo/bar/baz')-\u003efirstSegment('/'); // foo\nstring('foo/bar/baz')-\u003elastSegment('/'); // baz\n```\n\n## pop\n```php\n/**\n * Pop (remove) the last segment of a string based on a delimiter\n * \n * @param string $delimiter\n * \n * @return \\Spatie\\String\\String\n */\npublic function pop($delimiter)\n```\n\nExample:\n```php\nstring('foo/bar/baz')-\u003epop('/'); // foo/bar\nstring('foo/bar/baz')-\u003epop('/')-\u003epop('/'); // foo\n```\n\n## contains\n```php\n/**\n * Check whether a string contains a substring\n *\n * @param array|string $needle\n * @param bool $caseSensitive\n * @param bool $absolute\n *\n * @return bool\n */\npublic function contains($delimiter)\n```\n\nExample:\n```php\nstring('hello world')-\u003econtains('world'); // true\nstring('hello world')-\u003econtains('belgium'); // false\n```\n\n## Integration with underscore.php\nIn addition to the methods described above, you can also \nuse [all string methods](https://github.com/Anahkiasen/underscore-php/blob/master/src/Methods/StringsMethods.php) provided\nby [Maxime Fabre's underscore package](https://github.com/Anahkiasen/underscore-php).\n\nFor example:\n```php\nstring('i am a slug')-\u003eslugify()` // returns 'i-am-a-slug'\n``` \n\nOf course, you can chain underscore's methods with our own.\n```php\nstring('i am a slug')-\u003eslugify()-\u003ebetween('i', 'a-slug`) // returns '-am-'\n``` \n\nBe aware that some underscore methods do not return a string value. Such methods are not chainable.\n```php\nstring('freek@spatie.be')-\u003eisEmail() // returns true;\n``` \n\n## Testing\n\nYou can run the tests with:\n\n```bash\nvendor/bin/phpunit\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\n## About Spatie\n\nSpatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).\n\n## Alternatives\n\nThis package is primarily built for usage in our own projects. If you need a more full fledged string package take at look at these ones:\n- [Anahkiasen/underscore-php](https://github.com/Anahkiasen/underscore-php)\n- [danielstjules/Stringy](https://github.com/danielstjules/Stringy)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fstring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fstring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fstring/lists"}