Advertisement
FredrikPlays

Laravel PHP String Functions

Feb 6th, 2022
1,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.69 KB | None | 0 0
  1. <?php
  2.  
  3. // from https://youtu.be/_XS8kxTuq_Q
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use Illuminate\Support\Str;
  8.  
  9. class StringController extends Controller
  10. {
  11.     public function __invoke()
  12.     {
  13.         // 1. UPPERCASE/LOWERCASE LETTERS
  14.  
  15.         echo ucfirst('john');          // Result: "John"
  16.         echo '<br />';
  17.         echo lcfirst('John');          // Result: "john"
  18.         echo '<br />';
  19.         echo strtolower('John Smith'); // Result: "john smith"
  20.         echo '<br />';
  21.         echo strtoupper('john smith'); // Result: "JOHN SMITH"
  22.         echo '<br />';
  23.         echo ucwords('john smith');    // Result: "John Smith"
  24.         echo '<br />';
  25.  
  26.         // Laravel helpers:
  27.         echo Str::camel('variable_name');    // Result: "variableName"
  28.         echo '<br />';
  29.         echo Str::snake('variableName');     // Result: "variable_name"
  30.         echo '<br />';
  31.         echo Str::kebab('variableName');     // Result: "variable-name"
  32.         echo '<br />';
  33.         echo Str::slug('Some article title'); // Result: "some-article-title"
  34.         echo '<br />';
  35.  
  36.         echo '<hr />';
  37.  
  38.         // ----------------------------------------------
  39.  
  40.         // 2. TRIM / SUBSTRING
  41.  
  42.         echo '|' . trim(' With spaces ') . '|';  // Result: "With spaces"
  43.         echo '<br />';
  44.         echo '|' . ltrim(' With spaces ') . '|'; // Result: "With spaces "
  45.         echo '<br />';
  46.         echo '|' . rtrim(' With spaces ') . '|'; // Result: " With spaces"
  47.         echo '<br />';
  48.  
  49.         echo substr(' With spaces ', 1, 4); // Result: " With"
  50.         echo '<br />';
  51.         echo substr_replace(' With spaces ', 'Without', 1, 4);
  52.         // Result: " Without spaces "
  53.         echo '<br />';
  54.         echo substr_count('with spaces and with more', 'with');
  55.         // Result: 2
  56.         echo '<br />';
  57.  
  58.         // Laravel helpers:
  59.         echo Str::start('path/to/folder', '/'); // Result: "/path/to/folder"
  60.         echo '<br />';
  61.         echo Str::finish('/path/to/folder', '/'); // Result: "/path/to/folder/"
  62.         echo '<br />';
  63.  
  64.         echo '<hr />';
  65.  
  66.         // ----------------------------------------------
  67.  
  68.         // 3. CHECK IF STRING CONTAINS SOMETHING
  69.  
  70.         echo str_contains('Abc', 'a') ? 'true' : 'false';    // Result: "false"
  71.         echo '<br />';
  72.         echo str_starts_with('Abc', 'A') ? 'true' : 'false'; // Result: "true"
  73.         echo '<br />';
  74.         echo str_ends_with('Abc', 'c') ? 'true' : 'false';   // Result: "true"
  75.         echo '<br />';
  76.  
  77.         echo strstr('My Abc', 'a') ?: 'not found'; // Result: "not found"
  78.         echo '<br />';
  79.         echo stristr('My Abc', 'a') ?: 'false';    // Result: "Abc"
  80.         echo '<br />';
  81.  
  82.         echo strpos('Abcdefg', 'c');  // Result: 2
  83.         echo '<br />';
  84.         echo stripos('ABCdefg', 'b'); // Result: 1
  85.         echo '<br />';
  86.         echo strrpos('AbcAbc', 'c');  // Result: 5
  87.         echo '<br />';
  88.         echo strripos('AbcAbc', 'a'); // Result: 3
  89.         echo '<br />';
  90.  
  91.         // Laravel helpers:
  92.         echo Str::containsAll('Abcdefg', ['Abc', 'def']) ? 'true' : 'false';
  93.         // Result: "true"
  94.         echo '<br />';
  95.         echo Str::startsWith('Abc', ['A', 'c']) ? 'true' : 'false';
  96.         // Result: "true"
  97.         echo '<br />';
  98.         echo Str::endsWith('Abc', ['a', 'c']) ? 'true' : 'false';
  99.         // Result: "true"
  100.         echo '<br />';
  101.  
  102.         echo '<hr />';
  103.  
  104.         // ----------------------------------------------
  105.  
  106.         // 4. STRING LENGTH
  107.  
  108.         echo strlen('Some string'); // Result: 11
  109.         echo '<br />';
  110.         echo str_word_count('Some string'); // Result: 2
  111.         echo '<br />';
  112.  
  113.         echo '<hr />';
  114.  
  115.         // ----------------------------------------------
  116.  
  117.         // 5. PADDING AND MASKING
  118.  
  119.         echo str_pad('123', 7, '0', STR_PAD_LEFT);
  120.         // Result: 0000123
  121.         echo '<br />';
  122.  
  123.         // Laravel helpers:
  124.         echo Str::padLeft('123', 7, '0'); // Result: 0000123
  125.         echo '<br />';
  126.         echo Str::padRight('123', 7, '0'); // Result: 1230000
  127.         echo '<br />';
  128.         echo Str::padBoth('123', 7, '*'); // Result: **123**
  129.         echo '<br />';
  130.         echo Str::mask('1234567890123456', '*', 4);
  131.         // Result: 1234************
  132.         echo '<br />';
  133.         echo Str::mask('1234567890123456', '*', 0, -4);
  134.         // Result: ************3456
  135.         echo '<br />';
  136.  
  137.         echo '<hr />';
  138.  
  139.         // ----------------------------------------------
  140.  
  141.         // 6. RANDOM PHP FUNCTIONS
  142.  
  143.         echo number_format(123456, 2); // Result: 123,456.00
  144.         echo '<br />';
  145.  
  146.         echo sprintf('Some number %d with string %s', 123, 'Something');
  147.         // Result: "Some number 123 with string Something"
  148.         echo '<br />';
  149.  
  150.         print_r(str_getcsv("LEGO,19.99,Denmark"));
  151.         // Result: Array ( [0] => LEGO [1] => 19.99 [2] => Denmark )
  152.         echo '<br />';
  153.  
  154.         echo strrev('abcdef');
  155.         // Result: fedcba
  156.         echo '<br />';
  157.  
  158.         echo levenshtein('Goooogle', 'Google'); // Result: 2
  159.         echo '<br />';
  160.         echo similar_text('Goooogle', 'Google', $percentage);
  161.         echo ' ('.number_format($percentage, 2).'%)';
  162.         // Result: 6 (85.71%)
  163.         echo '<br />';
  164.  
  165.         echo '<hr />';
  166.  
  167.         // ----------------------------------------------
  168.  
  169.         // 7. RANDOM LARAVEL HELPERS
  170.  
  171.         echo Str::plural('person');     // Result: "people"
  172.         echo '<br />';
  173.         echo Str::singular('children'); // Result: "child"
  174.         echo '<br />';
  175.  
  176.         echo Str::random(12); // Result: "f2zujRZcLuJA"
  177.         echo '<br />';
  178.         echo Str::uuid(); // Result: "eb2afd13-035b-4b28-a131-bf4c1a390dcd"
  179.         echo '<br />';
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement