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.
You can install the to-string library via Composer. Run the following command in your project directory:
composer require effectra/to-stringThe classes have been refactored to be stateful. You instantiate them with a value and then call utility methods on them.
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. C4rN1QX7New 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.
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.
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-13New 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").
This library is licensed under the MIT License. See the LICENSE file for more information.
Contributions are welcome! If you have any improvements or bug fixes, please submit a pull request.
The to-string library is developed and maintained by Effectra.
For any questions or issues, please open an issue on GitHub.