effectra / to-string
The Effectra ToString package.
Requires (Dev)
- barryvdh/laravel-ide-helper: ^3.7
- illuminate/support: ^13.22
- mrpunyapal/peststan: ^0.2.12
- pestphp/pest: ^3.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.2
- spatie/laravel-package-tools: ^1.93
README
to-string is a PHP library that provides various utility classes for string manipulation and conversion. It offers functionality to convert text case, strip tags, generate slugs, format arrays, and handle date/time values.
Installation
You can install the to-string library via Composer. Run the following command in your project directory:
composer require effectra/to-string
Usage
The classes have been refactored to be stateful. You instantiate them with a value and then call utility methods on them.
TextToString
use Effectra\ToString\TextToString; $textToString = new TextToString('Hello, World!'); // Converts a string to uppercase. echo $textToString->toUppercase(); // Output: HELLO, WORLD! // Converts a string to lowercase. echo $textToString->toLowercase(); // Output: hello, world! // Strips HTML and PHP tags from a string. $textToString->setValue('<p>Hello, <b>World!</b></p>'); echo $textToString->strip(); // Output: Hello, World! // Converts a string to a URL-friendly slug. $textToString->setValue('Hello, World!'); echo $textToString->textToSlug(); // Output: hello-world // Generates a random text of specified length. echo $textToString->generateRandomText(8); // Output: e.g. C4rN1QX7
New Utility Methods:
toTitleCase(): Converts the string to Title Case.truncate(int $length = 100, string $append = '...'): Truncates the string to a specific length.contains(string $needle): Checks if the string contains a specific substring.
ArrayToString
use Effectra\ToString\ArrayToString; $arrayToString = new ArrayToString(['apple', 'banana', 'cherry']); // Converts an array to a string representation. echo $arrayToString->array(); // Output: ['apple', 'banana', 'cherry'] (formatted) // Converts an array to a string by joining its elements with a separator. echo $arrayToString->arrayToText(','); // Output: apple,banana,cherry // Converts an array to a URL-friendly slug by joining its elements. echo $arrayToString->arrayToSlug(); // Output: apple-banana-cherry // Converts an array to a key-value pair string representation. $arrayToString->setValue(['name' => 'John', 'age' => 25]); echo $arrayToString->arrayToTextKeyValue(); /* Output: name: John, age: 25, */
New Utility Methods:
toJson(): Converts the array to a JSON string.toXml(string $rootElement = 'root'): Converts the array to an XML representation.toQueryString(): Converts the array to a URL query string.
DateToString
use Effectra\ToString\DateToString; $dateToString = new DateToString(3600); // 3600 seconds (1 hour) // Formats a time value in seconds into HH:MM:SS format. echo $dateToString->formatTime(); // Output: 01:00:00 // Formats a timestamp into YYYY-MM-DD format. $dateToString->setValue(time()); echo $dateToString->formatDate(); // Output: e.g. 2023-05-13
New Utility Methods:
formatDateTime(): Formats the timestamp intoYYYY-MM-DD HH:MM:SS.timeAgo(): Returns a human-readable "time ago" string (e.g., "2 hours ago").
License
This library is licensed under the MIT License. See the LICENSE file for more information.
Contributing
Contributions are welcome! If you have any improvements or bug fixes, please submit a pull request.
Credits
The to-string library is developed and maintained by Effectra.
Support
For any questions or issues, please open an issue on GitHub.