Advertisement
vitozy

Laravel 7 - Fluent String

Mar 29th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. <?php
  2. //title case + trim
  3. $string = "    teszt cím      ";
  4. $native = ucwords(trim($string));
  5.  
  6. $fluent = (string)Str::of($string)
  7.     ->trim()
  8.     ->title();
  9.  
  10. echo $fluent; //Teszt Cím
  11.  
  12. //title case + replace + split
  13. $string = "egy kettő három";
  14.  
  15. $native = explode(";", str_replace(" ", ";", ucwords($string)));
  16.  
  17. $fluent = Str::of($string)
  18.     ->title()
  19.     ->replace(' ', ';')
  20.     ->explode(';');
  21.  
  22. var_dump($fluent); //Collection of ["Egy", "Kettő", "Három"]
  23. var_dump($fluent->toArray()); //["Egy", "Kettő", "Három"]
  24.  
  25. //prefix
  26. $string = "world";
  27.  
  28. $native = ucwords("hello " . $string);
  29.  
  30. $fluent = (string)Str::of($string)
  31.     ->start("hello ")
  32.     ->title();
  33.  
  34. echo $fluent; //Hello World
  35.  
  36. //cut prefix + split
  37. $string = "N#5642-5565-5421";
  38.  
  39. $native = explode('-', mb_substr($string, 2));
  40.  
  41. $fluent = Str::of($string)
  42.     ->after("N#")
  43.     ->explode("-");
  44.  
  45. var_dump($fluent); //Collection of [5642, 5565, 5421]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement