DEV Community

Morcos Gad
Morcos Gad

Posted on

More awesome Laravel - PHP String Functions

It is interesting to know that there are so many Functions that will help you in your next projects I found it from this source https://www.youtube.com/watch?v=_XS8kxTuq_Q and wanted to share this wonderful information with you
We use use Illuminate\Support\Str let's get them

// 1. UPPERCASE/LOWERCASE LETTERS

echo ucfirst('john');          // Result: "John"

echo lcfirst('John');          // Result: "john"

echo strtolower('John Smith'); // Result: "john smith"

echo strtoupper('john smith'); // Result: "JOHN SMITH"

echo ucwords('john smith');    // Result: "John Smith"


// Laravel helpers:

echo Str::camel('variable_name');    // Result: "variableName"

echo Str::snake('variableName');     // Result: "variable_name"

echo Str::kebab('variableName');     // Result: "variable-name"

echo Str::slug('Some article title'); // Result: "some-article-title"

// 2. TRIM / SUBSTRING

echo '|' . trim(' With spaces ') . '|';  // Result: "With spaces"

echo '|' . ltrim(' With spaces ') . '|'; // Result: "With spaces "

echo '|' . rtrim(' With spaces ') . '|'; // Result: " With spaces"

echo substr(' With spaces ', 1, 4); // Result: " With"

echo substr_replace(' With spaces ', 'Without', 1, 4); 
// Result: " Without spaces "

echo substr_count('with spaces and with more', 'with');  //Result: 2

// Laravel helpers:

echo Str::start('path/to/folder', '/'); // Result: "/path/to/folder"

echo Str::finish('/path/to/folder', '/'); // Result: "/path/to/folder/"

// 3. CHECK IF STRING CONTAINS SOMETHING

echo str_contains('Abc', 'a') ? 'true' : 'false';    // Result: "false"

echo str_starts_with('Abc', 'A') ? 'true' : 'false'; // Result: "true"

echo str_ends_with('Abc', 'c') ? 'true' : 'false';   // Result: "true"

echo strstr('My Abc', 'a') ?: 'not found'; // Result: "not found"

echo stristr('My Abc', 'a') ?: 'false';    // Result: "Abc"

echo strpos('Abcdefg', 'c');  // Result: 2

echo stripos('ABCdefg', 'b'); // Result: 1

echo strrpos('AbcAbc', 'c');  // Result: 5

echo strripos('AbcAbc', 'a'); // Result: 3

// Laravel helpers:

echo Str::containsAll('Abcdefg', ['Abc', 'def']) ? 'true' : 'false'; 
// Result: "true"

echo Str::startsWith('Abc', ['A', 'c']) ? 'true' : 'false'; 
// Result: "true"

echo Str::endsWith('Abc', ['a', 'c']) ? 'true' : 'false'; 
// Result: "true"

// 4. STRING LENGTH

echo strlen('Some string'); // Result: 11

echo str_word_count('Some string'); // Result: 2

// 5. PADDING AND MASKING

echo str_pad('123', 7, '0', STR_PAD_LEFT); // Result: 0000123

// Laravel helpers:

echo Str::padLeft('123', 7, '0'); // Result: 0000123

echo Str::padRight('123', 7, '0'); // Result: 1230000

echo Str::padBoth('123', 7, '*'); // Result: **123**

echo Str::mask('1234567890123456', '*', 4); 
// Result: 1234************

echo Str::mask('1234567890123456', '*', 0, -4);
// Result: ************3456

// 6. RANDOM PHP FUNCTIONS

echo number_format(123456, 2); // Result: 123,456.00

echo sprintf('Some number %d with string %s', 123, 'Something');
// Result: "Some number 123 with string Something"

print_r(str_getcsv("LEGO,19.99,Denmark"));
// Result: Array ( [0] => LEGO [1] => 19.99 [2] => Denmark )

echo strrev('abcdef'); // Result: fedcba

echo levenshtein('Goooogle', 'Google'); // Result: 2

echo similar_text('Goooogle', 'Google', $percentage);
echo ' ('.number_format($percentage, 2).'%)';
// Result: 6 (85.71%)

// 7. RANDOM LARAVEL HELPERS

echo Str::plural('person');     // Result: "people"

echo Str::singular('children'); // Result: "child"

echo Str::random(12); // Result: "f2zujRZcLuJA"

echo Str::uuid(); 
// Result: "eb2afd13-035b-4b28-a131-bf4c1a390dcd"

Enter fullscreen mode Exit fullscreen mode

I hope you enjoyed the code as much as I enjoyed sharing it with you.

Latest comments (0)