PHP code example of pointybeard / helpers-functions-strings

1. Go to this page and download the library: Download pointybeard/helpers-functions-strings library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pointybeard / helpers-functions-strings example snippets




declare(strict_types=1);

ns\Strings;

var_dump(Strings\utf8_wordwrap(
    'Some long string that we want to wrap at 20 characeters',
    20,
    PHP_EOL,
    true
));
// string(55) "Some long string
// that we want to wrap
// at 20 characeters"

var_dump(Strings\utf8_wordwrap_array(
    'Some long string that we want to wrap at 20 characeters',
    20,
    PHP_EOL,
    true
));
// array(3) {
//   [0] => string(16) "Some long string"
//   [1] => string(20) "that we want to wrap"
//   [2] => string(17) "at 20 characeters"
// }

var_dump(Strings\type_sensitive_strval(true));
// string(4) "true"
//
var_dump(Strings\type_sensitive_strval([1, 2, 3, 4]));
// string(5) "array"

var_dump(Strings\type_sensitive_strval(new \stdClass()));
// string(6) "object"

var_dump(Strings\mb_str_pad('Apple', 11, 'àèò', STR_PAD_LEFT, 'UTF-8'));
// string(17) "àèòàèòApple"

var_dump(Strings\mb_str_pad('Banana', 11, 'àèò', STR_PAD_RIGHT, 'UTF-8'));
// string(16) "Bananaàèòàè"

var_dump(Strings\mb_str_pad('Pear', 11, 'àèò', STR_PAD_BOTH, 'UTF-8'));
// string(18) "àèòPearàèòà"

var_dump(Strings\replace_placeholders_in_string(
    ['FIRSTNAME', 'LASTNAME', 'EMAILADDRESS'],
    ['Sarah', 'Smith', '[email protected]'],
    'My name is {{FIRSTNAME}} {{LASTNAME}}. Contact me at {{EMAILADDRESS}}.'
));
// string(62) "My name is Sarah Smith. Contact me at [email protected]."

var_dump(Strings\replace_placeholders_in_string(
    ['ONE', 'TWO', 'THREE', 'FOUR'],
    ['apple', 'banana', 'orange', 'banana'],
    '[ONE], [TWO], [THREE], [NOPE]',
    true,
    '[',
    ']'
));
// string(23) "apple, banana, orange, "

var_dump(Strings\random_string(15));
// string(15) "cTAPWAi2EOCop2N"

try {
    var_dump(Strings\random_string(8, '@[^-]@i'));
} catch (Error $ex) {
    echo 'Error generating random string. returned: '.$ex->getMessage().PHP_EOL;
}
// Error generating random string. returned: minimal characters generated. filter '@[^-]@i' might be too restrictive

var_dump(Strings\random_unique_classname('test', '\\MyApp'));
// string(36) "testOIXwzi9D6bAbvy5y9QYoayS2kabbBh56"

var_dump(Strings\encode_ampersands("10 &lt; 15 & 5 &gt; 1. &#x1234;"));
// string(35) "10 &lt; 15 &amp; 5 &gt; 1. &#x1234;"

var_dump(Strings\encode_ampersands('Apples & bananàs and pickles &amp; cheese.'));
// string(47) "Apples &amp; bananàs and pickles &amp; cheese."