Recommend this page to a friend! |
Download |
Info | Documentation | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2024-08-10 (5 days ago) | Not yet rated by the users | Total: 27 This week: 27 | All time: 11,157 This week: 4 |
Version | License | PHP version | Categories | |||
php-string-list 1.0.0 | MIT/X Consortium ... | 8.0 | Text processing, Validation, Parsers, P... |
Description | Author | |
This class can manipulate strings that contain multiple strings. |
A string list is a string form that represents an array in a specific format.
This format allows for easy conversion between string and array forms.
This can be useful for handling comma-separated strings and performing conversions, validations, or configuration of simple data storage, in scenarios where data needs to be easily readable and writable in a string format.
Here are some examples demonstrating the use of the Lists
class methods:
Convert Array to List:
$array = ['foo', 'bar', 'baz'];
$list = Lists::toList($array);
// Output: 'foo,bar,baz'
Convert List to Array:
$list = 'foo,bar,baz';
$array = Lists::toArray($list);
// Output: ['foo', 'bar', 'baz']
Check Valid List:
$list = 'foo=bar,bar=2,baz=[1;2;3]';
$isValid = Lists::isList($list);
// Output: true
Handle Nested Arrays:
$list = 'foo=bar,[address=[New York;city=NY]]';
$array = Lists::toArray($list);
// Output: ['foo' => 'bar', 'address' => ['New York', 'city' => 'NY']]
The class provides utility methods for manipulating and validating string lists, converting between string representations and arrays. It includes methods to check if a string is a valid list format, convert between list strings and arrays, and perform other related operations.
Install via composer
composer require peterujah/php-string-list
A string list is a string form that represents an array in a specific format. This format allows for easy conversion between string and array forms. This can be useful for handling comma-separated strings and performing conversions, validations or configuration of simple data storage, in a scenarios where data needs to be easily readable and writable in a string format.
*
A valid string list in this context follows specific formats:
Comma-Separated Non-Nested Arrays: Use commas to separate elements in a flat array both key-value pairs.
$list = 'foo,bar,baz=100';
$array = ['foo', 'bar', 'baz' => 100];
Nested Arrays Separation: Use semicolons for elements within nested arrays both key-value pairs.
$list = 'foo=bar,bar=2,baz=[1;2;3]';
$array = [
'foo' => 'bar',
'bar' => 2,
'baz' => [1, 2, 3]
];
- Key-Value Pair: Use `key=value` syntax. - Nested Arrays: Use square brackets `[]` and semicolons `;` for nested values.
*
While PHP?s built-in implode
and explode
functions are useful for basic string manipulation, the Lists
class provides additional functionality that addresses more complex requirements for working with string representations of arrays. Here?s what makes the Lists
class more powerful:
PHP?s explode
and implode
:
Cannot process arrays within arrays.
PHP Explode:
With php explode, the output is not as expected.
$list = 'foo=bar,bar=2,baz=[1;2;3]';
$array = explode(',', $list);
//Output:
Array
(
[0] => foo=bar
[1] => bar=2
[2] => baz=[1;2;3]
)
Lists Class:
Converts complex string structures into multidimensional arrays.
use \Peterujah\Strings\Lists;
$list = 'foo=bar,bar=2,baz=[1;2;3]';
$array = Lists::toArray($list);
// Output:
Array
(
[foo] => bar
[bar] => 2
[baz] => Array (
[0] => 1
[1] => 2
[2] => 3
)
)
Multidimensional Array
use \Peterujah\Strings\Lists;
$list = 'name=Peter,age=33,address=[country=Nigeria;city=EN]';
$array = Lists::toArray($list);
// Output: ['name' => 'Peter', 'age' => 33, 'address' => ['country' => 'Nigeria', 'city' => 'EN']]
*
PHP?s explode
and implode
:
Lists Class:
*
PHP?s explode
and implode
:
Lists Class:
Ensures adherence to rules for separators and nesting.
use \Peterujah\Strings\Lists;
$list = 'foo=bar,bar=2,baz=[1;2;3]';
$isValid = Lists::isList($list);
// Output: true
*
PHP?s explode
and implode
:
Lists Class:
*
PHP?s implode
:
Cannot handle multidimensional or associative arrays directly.
> Will encounter a `TypeError` because implode expects a simple array of values, not an associative array or multidimensional array.
$array = [
'name' => 'Peter',
'age' => 33,
'address' => [
'country' => 'Nigeria',
'city' => 'EN'
]
];
$list = implode(',', $array); // Results in TypeError
Lists Class:
Handles various data types and nested arrays gracefully.
use \Peterujah\Strings\Lists;
$array = [
'name' => 'Peter',
'age' => 33,
'address' => [
'country' => 'Nigeria',
'city' => 'EN'
]
];
$list = Lists::toList($array);
// Output: 'name=Peter,age=33,address=[country=Nigeria;city=EN]'
*
Determines if a string is a valid list based on the expected list format.
public static isList(string $list): bool
Parameters:
| Parameter | Type | Description |
|-----------|------|-------------|
| $list
| string | The string to check. |
Return Value:
bool
- Return true if the string is a valid list; otherwise, false.
*
Converts a string list to its original array structure.
public static toArray(string $list): array
Parameters:
| Parameter | Type | Description |
|-----------|------|-------------|
| $list
| string | The string list to convert. |
Return Value:
array
- Return the extracted array.
Throws:
*
Builds a string representation of an array.
public static toList(array $array, string $delimiter = ','): string
Parameters:
| Parameter | Type | Description |
|-----------|------|-------------|
| $array
| array | The input array to convert. |
| $delimiter
| string | The delimiter to use between (default: ',').<br />- For none nested array use ,
.<br />- For nested array use ;
. |
Return Value:
string
- Return the resulting string representation of the array.
Throws:
> It's recommended to leave the default delimiter to automatically decide which one to use.
*
Here are some examples demonstrating the use of the Lists
class methods:
Convert Array to List:
$array = ['foo', 'bar', 'baz'];
$list = Lists::toList($array);
// Output: 'foo,bar,baz'
Convert List to Array:
$list = 'foo,bar,baz';
$array = Lists::toArray($list);
// Output: ['foo', 'bar', 'baz']
Check Valid List:
$list = 'foo=bar,bar=2,baz=[1;2;3]';
$isValid = Lists::isList($list);
// Output: true
Handle Nested Arrays:
$list = 'foo=bar,[address=[New York;city=NY]]';
$array = Lists::toArray($list);
// Output: ['foo' => 'bar', 'address' => ['New York', 'city' => 'NY']]
Files (5) |
File | Role | Description | ||
---|---|---|---|---|
src (1 file) | ||||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
package.json | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.