Download the PHP package itools/smartstring without Composer
On this page you can find all versions of the php package itools/smartstring. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download itools/smartstring
More information about itools/smartstring
Files in itools/smartstring
Package smartstring
Short Description A powerful, web-centric string manipulation library with automatic HTML encoding and fluent interface for PHP
License MIT
Homepage https://github.com/interactivetools-com/SmartString
Informations about the package smartstring
SmartString: Secure and Simple String Handling for PHP
SmartString is a PHP string handling library that lets you write cleaner, simpler, more secure code faster and with less effort.
Instead of writing code like this:
You can write code like this:
SmartString handles HTML encoding automatically and provides utility functions for common tasks. This makes your code cleaner, more readable, and inherently more secure.
Table of Contents
- Quick Start
- Features and Usage Examples
- Creating SmartStrings
- Fluent Chainable Interface
- Automatic HTML-encoding
- Accessing Values
- Working with SmartArrays
- Type Conversion
- Encoding Values
- String Manipulation
- Number Formatting
- Date Formatting
- Phone Number Formatting
- Numeric Operations
- Conditional Operations
- Custom Functions
- Developer Debugging & Help
- Customizing Defaults
- Method Reference
- Questions?
Quick Start
Install via Composer:
Start using SmartString:
Features and Usage Examples
Creating SmartStrings
SmartString offers simple ways to create objects from various data types. It can be especially
useful to convert $_REQUEST
, or database record arrays to SmartString objects.
The automatic HTML-encoding feature means you don't need to call htmlspecialchars()
over and over again, and you get access to a variety of utility methods for common tasks.
Fluent Chainable Interface
SmartString provides a fluent, chainable interface that allows you to perform one or more operations in a single, readable line of code.
The fluent chainable interface allows you to build complex transformations step-by-step, making your code more intuitive and easier to read and maintain.
Automatic HTML-encoding
SmartString prioritizes web security by automatically HTML-encoding output by default. This greatly simplifies your code and helps prevent Cross-Site Scripting (XSS) vulnerabilities.
Whenever you use a SmartString object in a string context, it automatically HTML-encodes the output:
`
Accessing Values
You can access the original value with the value()
method:
Or you can also use the noEncode()
alias method for readability. This is useful when you have WYSIWYG content
that you don't want to double-encode, and you want to make it clear that the value is not encoded:
Working with SmartArrays
When you convert an array to SmartArray, you can use it like both an array and an object.
Type Conversion
You can convert SmartString objects to different types using terminal methods. This can be useful when you need to pass a SmartString value to a function that expects a specific type.
Encoding Values
Besides just HTML encoding, SmartString provides methods for explicit encoding in different scenarios:
String Manipulation
SmartString offers a variety of methods for common string operations, making it easy to modify and format your text:
And all of the above methods are chainable:
`
Number Formatting
SmartString provides a simple way to format numbers specifying the number of decimals and separators as needed. You can customize the default formats at the top of your script or in an init file to use throughout your application.
Date Formatting
You can format dates with a standard format for date, datetime, or specify a custom format as needed. You can customize the default formats at the top of your script or in an init file to use throughout your application.
You can find a list of available date formats in the PHP documentation: https://www.php.net/manual/en/datetime.format.php
Phone Number Formatting
SmartString formats phone numbers using customizable rules. You can customize the default formats at the top of your script or in an init file to use throughout your application.
Numeric Operations
SmartString provides a set of methods for performing basic arithmetic and percentage calculations. These methods are chainable, allowing for complex calculations to be expressed clearly and concisely:
Note: Be aware of decimal precision issues when performing calculations. Results may sometimes differ slightly from expected values due to floating-point arithmetic limitations inherit in all programming languages.
For more information, see PHP Floating Point Numbers.
Conditional Operations
Conditional operations provide a simple way to provide a fallback value when the current value is falsy, blank, null, or zero.
Custom Functions
SmartString provides an apply()
method that allows you to use custom code or PHP's built-in functions.
The value of the SmartString object is passed as the first argument to the callback function
followed by any supplied arguments.
Examples of using apply():
Developer Debugging & Help
When you call print_r() on a SmartString object, it will display the original value:
Calling SmartString::help()
or $obj->help()
will display a list of available methods and examples:
Customizing Defaults
You can customize the defaults by adding the following to the top of your script or in an init file:
Method Reference
In addition to the methods below, you can customize the defaults by adding the following to the top of your script or in an init file:
Basic Usage | |
---|---|
SmartString::new(\$value) | Creates a new SmartString object from a single value |
SmartArray::new(\$array) | Creates a new SmartArray from a regular PHP array. All nested arrays and values are converted to SmartArray and SmartString objects |
value() | Returns the original, unencoded value |
Type Conversion | |
int() | Returns the value as an integer |
float() | Returns the value as a float |
bool() | Returns the value as a boolean |
string() | Returns the value as a string (original value, not HTML-encoded) |
Encoding Methods | |
htmlEncode() | Returns HTML-encoded string |
urlEncode() | Returns URL-encoded string |
jsonEncode() | Returns JSON-encoded string |
noEncode() | Alias for value(), useful for readability in string contexts |
String Manipulation | |
textOnly() | Removes HTML tags from the string, decodes entities, and trims whitespace |
nl2br() | Converts newlines to HTML line breaks |
trim() | Trims whitespace or specified characters from the string |
maxWords(\$max, \$ellipsis = '...') | Limits the string to a specific number of words |
maxChars(\$max, \$ellipsis = '...') | Limits the string to a specific number of characters |
Formatting | |
dateFormat(\$format = default) | Formats the value as a date, using default or specified date format |
dateTimeFormat(\$format = default) | Formats the value as a date and time, using default or specified date format |
numberFormat(\$decimals = 0) | Formats the value as a number |
phoneFormat() | Formats the value as a phone number |
Numeric Operations | |
percent(\$decimals = 0) | Converts the value to a percentage |
percentOf(\$total, \$decimals = 0) | Calculates the percentage of the value relative to the given total |
add(\$value) | Adds the given value to the current value |
subtract(\$value) | Subtracts the given value from the current value |
multiply(\$value) | Multiplies the current value by the given value |
divide(\$divisor) | Divides the current value by the given divisor |
Conditional Operations | |
or(\$fallback) | Returns the fallback if the current value is falsy |
ifNull(\$fallback) | Returns the fallback if the current value is null |
ifBlank(\$fallback) | Returns the fallback if the current value is an empty string |
ifZero(\$fallback) | Returns the fallback if the current value is zero |
Miscellaneous | |
apply(\$func, ...\$args) | Applies a custom function to the value |
help() | Displays help information about available methods |
See Also: For array operations, check out our companion library SmartArray
,
powerful array operations with chainable methods and seamless SmartString
integration:
https://github.com/interactivetools-com/SmartArray?tab=readme-ov-file#method-reference
Questions?
This library was developed for CMS Builder, post a message in our "CMS Builder" forum here: https://www.interactivetools.com/forum/