PHP code example of robertaodj / str

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

    

robertaodj / str example snippets


use Robertaodj\Str\Str;

// Create a string instance
$result = Str::of("  Hello World!  ")
    ->trim()
    ->lower()
    ->studly()
    ->get(); // "HelloWorld!"

// Static methods
echo Str::slug("Olá Mundo"); // "ola-mundo"
echo Str::kebab("HelloWorld"); // "hello-world"
echo Str::mask("12345678901", "###.###.###-##"); // "123.456.789-01"

use Robertaodj\Str\Str;

// Text cleaning and formatting
$text = Str::of("  Texto com acentos e símbolos!@#  ")
    ->trim()
    ->removeAccents()
    ->alphanumeric(' ')
    ->title()
    ->get(); // "Texto Com Acentos E Simbolos"

// Case conversions
echo Str::camel("hello-world"); // "helloWorld"
echo Str::snake("HelloWorld"); // "hello_world"
echo Str::studly("hello_world"); // "HelloWorld"

// Document formatting
echo Str::mask("12345678901", "cpf"); // "123.456.789-01"
echo Str::mask("12345678000195", "cnpj"); // "12.345.678/0001-95"

// Capitalization with Portuguese rules
echo Str::of("o gato subiu no telhado da casa")
    ->capitalize('pt')
    ->get(); // "O Gato Subiu no Telhado da Casa"

// Content validation
$html = "<p>Hello <b>World</b></p>";
echo Str::of($html)->isHtml()->get(); // true
echo Str::of($html)->stripTags()->get(); // "Hello World"

// Text truncation
$longText = "Lorem ipsum dolor sit amet...";
echo Str::of($longText)->limit(10)->get(); // "Lorem ipsu..."
echo Str::of($longText)->words(3)->get(); // "Lorem ipsum dolor..."