PHP code example of srcoder / normalize-strings

1. Go to this page and download the library: Download srcoder/normalize-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/ */

    

srcoder / normalize-strings example snippets


$normalizer = new Srcoder\Normalize\Normalize([
    new Srcoder\Normalize\Rule\Uppercase,
    new Srcoder\Normalize\Rule\Append(' World'),
    new Srcoder\Normalize\Rule\Append('!')
]);

echo $normalizer->normalize('Hello');
// "HELLO World!"

// Adding rules
$normalizer->addRule(new Srcoder\Normalize\Rule\Replace('HELLO', 'Bye'));

echo $normalizer->normalize('Hello');
// "Bye World!"

// ... continue

$newNormalizer = new Srcoder\Normalize\Normalize([
    new Srcoder\Normalize\Rule\Prepend('Good')
]);

echo $newNormalizer->normalize('Hello');
// "GoodHello"

// Set chain
$newNormalizer->setChain($normalizer);

echo $newNormalizer->normalize('Hello');
// "GoodBye World!"

// ... continue

$newNormalizer->prependRule(new Srcoder\Normalize\RegExp('#[A-Z]+#', '+'));

echo $newNormalizer->normalize('Hello');
// "GoodBye+ELLO World!"

class sha1Rule implements Sroder\Normalize\Rule\RuleInterface
{
    
    public function apply(string $string) : string
    {
        return sha1($strings);    
    }

}

$myNormalizer = new Sroder\Normalize([
    new sha1Rule
]);

echo $myNormalizer->normalize('test');
// "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"

// ... continue

$normalizeManager = new Srcoder\Normalize\Manager();
// or static
$normalizeManager = Srcoder\Normalize\Manager::instance();

// Add already defined normalizer
$normalizeManager->add($normalizer, 'helloworld');

// createAndAdd
$normalizeManager->createAndAdd(
        [ // Rules
            new Srcoder\Normalize\Rule\RegExp("#[_ ]*([A-Z])#", "_\\1"),
            new Srcoder\Normalize\Rule\Trim("_ \t\n\r\0\x0B"),
            new Srcoder\Normalize\Rule\Lowercase()
        ],
        'underscore' // identifier
        //, 'helloworld' // chain
);

// ... snip to somewhere else

echo $normalizeManager->get('helloworld')
        ->normalize('Ibiza!');
// "Bye Ibiza!!"

echo Srcoder\Normalize\Manager::instance()
        ->get('underscore')
        ->normalize('HelloWorld');
// "hello_world"

class MyAwesomeClass
{
    use \Srcoder\Normalize\NormalizeTrait;
    
    public function __construct()
    {
        $this->normalizerInit();
        
        $this->addNormalizeRules(
                [
                    new Srcoder\Normalize\Rule\Trim('Ho'),
                    new Srcoder\Normalize\Rule\Uppercase,
                ]
        );
    }
}

$myAwesome = new MyAwesomeClass;

echo $myAwesome->normalize('Hello');
// "ELL"