Download the PHP package vielhuber/stringhelper without Composer
On this page you can find all versions of the php package vielhuber/stringhelper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vielhuber/stringhelper
More information about vielhuber/stringhelper
Files in vielhuber/stringhelper
Package stringhelper
Short Description Collection of useful shorthand string related functions in PHP.
License MIT
Informations about the package stringhelper
⛏️ stringhelper ⛏️
motivation
this package is a collection of various helpers for string manipulation, validation, and utility functions designed to simplify the life of php developers. it streamlines tasks like checking variable existence, comparing values, and handling dates, urls, and emails. this tool enhances efficiency by offering concise, reusable methods for everyday coding challenges.
installation
usage
existence
equality
value
loop
try
A short note on the usage of @: In this concept we use @-operator that hides errors. We are aware of its potential misuse and also of its benefits.
Be careful when using @\$a['undefined'], there can be 2 possible errors: a missing variable or a missing index. In both cases, we intentionally prevent the parser from stopping and catch the resulting null value. Be aware: If $a is a string, @\$a['undefined'] evaluates to $a[0] since php coerces 'undefined' to 0 and therefore exists.
Another general rule of thumb: Don't use the operator before function calls (@__x(\$a['undefined']).
A caveat is that the @-operator does not catch any fatal runtime errors since PHP 8 anymore.\ For that there is also another more sophisticated way of checking the existence of variables:
If $var
is totally undefined, the following expressions evaluate correctly to false:
Be aware that arrow functions are only available from php 7.4; Prior versions should use:
classes
helpers
there are also some other neat little helpers available.
usage as class
if you don't like hotloaded functions, you also can use this library in a class-based way:
js implementation
there is also a javascript implemenation hlp with similiar functions available.
testing
copy .env.example
to .env
, fill in values, install dependencies with composer install
and run ./vendor/bin/phpunit
.
appendix
existence matrix
__x() | __true() | __false() | !== null | != null | !== false | != false | === true | == true | !is_null() | isset() | !empty() | if/else | ?true:false | (??true) === true | (??true) == true | (??true) === false | (??true) == false | count() > 0 | != '' | !== '' | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
null | false | false | false | false | false | true | false | false | false | false | false | false | false | false | true | true | false | false | false | false | true |
false | false | false | true | true | false | false | false | false | false | true | true | false | false | false | false | false | true | true | true | false | true |
true | true | true | false | true | true | true | true | true | true | true | true | true | true | true | true | true | false | false | true | true | true |
[] | false | false | false | true | false | true | false | false | false | true | true | false | false | false | false | false | false | false | false | true | true |
[''] | false | false | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
0 | true | false | true | true | false | true | false | false | false | true | true | false | false | false | false | false | false | false | true | false | true |
1 | true | true | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
-1 | true | true | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'0' | true | false | true | true | true | true | false | false | false | true | true | false | false | false | false | false | false | false | true | true | true |
'1' | true | true | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'-1' | true | true | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'' | false | false | false | true | false | true | false | false | false | true | true | false | false | false | false | false | false | false | true | false | false |
' ' | false | false | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'null' | true | false | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'false' | true | false | true | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'true' | true | true | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'str' | true | true | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
[0,1] | true | true | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
[0] | true | true | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'a:0:{}' | false | false | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'b:1;' | true | true | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
'b:0;' | false | false | true | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
new stdClass | false | false | false | true | true | true | true | false | true | true | true | true | true | true | false | true | false | false | true | true | true |
$_GET['undefined'] | error | error | error | error | error | error | error | error | error | error | false | false | error | error | false | true | false | false | error | error | error |
sub>@$\_GET['undefined'] | false | false | false | false | false | true | false | false | false | false | false | false | false | false | true | true | false | false | false | false | true |
loose comparison matrix
== | null | false | true | [] | [''] | 0 | 1 | -1 | '0' | '1' | '-1' | '' | ' ' | 'null' | 'false' | 'true' | 'str' | [0,1] | [0] | 'a:0:{}' | 'b:1;' | 'b:0;' | new stdClass | \$_GET['undefined'] | sub>@\$\_GET['undefined'] |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
null | true | true | false | true | false | true | false | false | false | false | false | true | false | false | false | false | false | false | false | false | false | false | false | error | true |
false | true | true | false | true | false | true | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | false | error | true |
true | false | false | true | false | true | false | true | true | false | true | true | false | true | true | true | true | true | true | true | true | true | true | true | error | false |
[] | true | true | false | true | false | false | false | false | false | false | false | false | false | false | false | false | false | false | false | false | false | false | false | error | true |
[''] | false | false | true | false | true | false | false | false | false | false | false | false | false | false | false | false | false | false | true | false | false | false | false | error | false |
0 | true | true | false | false | false | true | false | false | true | false | false | true | true | true | true | true | true | false | false | true | true | true | error | error | true |
1 | false | false | true | false | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | false | false | error | error | false |
-1 | false | false | true | false | false | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | false | error | error | false |
'0' | false | true | false | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | false | false | false | false | error | false |
'1' | false | false | true | false | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | false | false | false | error | false |
'-1' | false | false | true | false | false | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | false | false | error | false |
'' | true | true | false | false | false | true | false | false | false | false | false | true | false | false | false | false | false | false | false | false | false | false | false | error | true |
' ' | false | false | true | false | false | true | false | false | false | false | false | false | true | false | false | false | false | false | false | false | false | false | false | error | false |
'null' | false | false | true | false | false | true | false | false | false | false | false | false | false | true | false | false | false | false | false | false | false | false | false | error | false |
'false' | false | false | true | false | false | true | false | false | false | false | false | false | false | false | true | false | false | false | false | false | false | false | false | error | false |
'true' | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | true | false | false | false | false | false | false | false | error | false |
'str' | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | true | false | false | false | false | false | false | error | false |
[0,1] | false | false | true | false | false | false | false | false | false | false | false | false | false | false | false | false | false | true | false | false | false | false | false | error | false |
[0] | false | false | true | false | true | false | false | false | false | false | false | false | false | false | false | false | false | false | true | false | false | false | false | error | false |
'a:0:{}' | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | false | false | false | true | false | false | false | error | false |
'b:1;' | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | false | false | false | false | true | false | false | error | false |
'b:0;' | false | false | true | false | false | true | false | false | false | false | false | false | false | false | false | false | false | false | false | false | false | true | false | error | false |
new stdClass | false | false | true | false | false | error | error | error | false | false | false | false | false | false | false | false | false | false | false | false | false | false | true | error | false |
\$_GET['undefined'] | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error | error |
sub>@\$\_GET['undefined'] | true | true | false | true | false | true | false | false | false | false | false | true | false | false | false | false | false | false | false | false | false | false | false | error | true |