String useful manipulation

  1. capitalize
  2. fromBase64
  3. limit
  4. normalize
  5. onlyAlpha
  6. onlyNumbers
  7. padL
  8. padR
  9. sub
  10. replace
  11. replaceRegex
  12. reverse
  13. toBase64
  14. toLower
  15. toUpper
  16. toUpperFirst
  17. trim
  18. ltrim
  19. rtrim
  20. withoutSpaces

capitalize

Capitalize each word

$string = 'hello world';
$string = SM::capitalize($string);
// or
$string = SM::make($string)
    ->capitalize();
echo $string; // Hello World

fromBase64

Decode from base64

$string = 'SGVsbG8gd29ybGQ=';
$string = SM::fromBase64($string);
// or
$string = SM::make($string)
    ->fromBase64();
echo $string; // Hello world

limit

Limit the string length.

If $end is setted, it will be added at the end of string.

If $length is less than the length of $end, $end will be the new string.

$string = 'The quick brown fox jumps over the lazy dog';
$string = SM::limit($string, 30);
// or
$string = SM::make($string)
    ->limit(30);
echo $string; // The quick brown fox jumps over

//if you use $end
$string = 'The quick brown fox jumps over the lazy dog';
$string = SM::limit($string, 30, '[...]');
// or
$string = SM::make($string)
    ->limit(30, '[...]');
echo $string; // The quick brown fox jumps[...]

normalize

Remove all accents and special characters.

Only alphanumeric and spaces are preserved

$string = 'âäàåçêëèïîìÆôöòûùÿ';
$string = SM::normalize($string);
// or
$string = SM::make($string)
    ->normalize();
echo $string; // aaaaceeeiiiooouuy

onlyAlpha

Only keep letters

$string = 'Hello 123 world';
$string = SM::onlyAlpha($string);
// or
$string = SM::make($string)
    ->onlyAlpha();
echo $string; // Helloworld

onlyNumbers

Only keep numbers

$string = 'Hello 123 world';
$string = SM::onlyNumbers($string);
// or
$string = SM::make($string)
    ->onlyNumbers();
echo $string; // 123

padL

Repeatedly add a character to the left of the string until the length is reached.

If the string is equal to or greater than the length, nothing is changed.

$string = 'abc123';
$string = SM::padL($string, 8, 'z');
// or
$string = SM::make($string)
    ->padL(8, 'z');
echo $string; // zzabc123

padR

Repeatedly add a character to the right of the string until the length is reached.

If the string is equal to or greater than the length, nothing is changed.

$string = 'abc123';
$string = SM::padR($string, 8, 'z');
// or
$string = SM::make($string)
    ->padR(8, 'z');
echo $string; // abc123zz

sub

Performs substring

$string = 'Hello world';
$string = SM::sub($string, 3, 5);
// or
$string = SM::make($string)
    ->sub(3, 5);
echo $string; // lo wo

replace

Replace all occurrences of a string.

$string = 'The quick brown fox jumps';
$string = SM::replace($string, 'fox', 'dog');
// or
$string = SM::make($string)
    ->replace('fox', 'dog');
echo $string; // The quick brown dog jumps

// case insensitive:
$string = 'The quick brown Fox jumps';
$string = SM::replace($string, 'fox', 'Dog', false);
// or
$string = SM::make($string)
    ->replace('fox', 'Dog', false);
echo $string; // The quick brown Dog jumps

replaceRegex

Replace all occurrences of a regex.

$string = 'The  quick   brown    fox jumps';
$string = SM::replaceRegex($string, '/\s+/', ' ');
// or
$string = SM::make($string)
    ->replaceRegex('/\s+/', ' ');
echo $string; // The quick brown fox jumps

reverse

Reverse the string

$string = 'spmuj xof nworb kciuq ehT';
$string = SM::reverse($string);
// or
$string = SM::make($string)
    ->reverse();
echo $string; // The quick brown fox jumps

toBase64

Encode to base64

$string = 'Hello world';
$string = SM::toBase64($string);
// or
$string = SM::make($string)
    ->toBase64();
echo $string; // SGVsbG8gd29ybGQ=

toLower

Morph to lower case.

$string = 'HeLlO wOrLd';
$string = SM::toLower($string);
// or
$string = SM::make($string)
    ->toLower();
echo $string; // hello world

toUpper

Morph to upper case.

$string = 'HeLlO wOrLd';
$string = SM::toUpper($string);
// or
$string = SM::make($string)
    ->toUpper();
echo $string; // HELLO WORLD

toUpperFirst

Morph to upper case the first letter of string.

$string = 'hello world';
$string = SM::toUpperFirst($string);
// or
$string = SM::make($string)
    ->toUpperFirst();
echo $string; // Hello world

trim

Remove all spaces from the start and end.

$string = ' Hello world ';
$string = SM::trim($string);
// or
$string = SM::make($string)
    ->trim();
echo $string; // Hello world

// you can set what caracters you can trim
$string = SM::trim($string, " \n\r\t\v\0");
$string = SM::make($string)
    ->trim(" \n\r\t\v\0");

ltrim

Remove all spaces from the start and end.

$string = ' Hello world ';
$string = SM::ltrim($string);
// or
$string = SM::make($string)
    ->ltrim();
echo $string; // Hello world 

// you can set what caracters you can ltrim
$string = SM::ltrim($string, " \n\r\t\v\0");
$string = SM::make($string)
    ->ltrim(" \n\r\t\v\0");

rtrim

Remove all spaces from the start and end.

$string = ' Hello world ';
$string = SM::rtrim($string);
// or
$string = SM::make($string)
    ->rtrim();
echo $string; //  Hello world

// you can set what caracters you can rtrim
$string = SM::rtrim($string, " \n\r\t\v\0");
$string = SM::make($string)
    ->rtrim(" \n\r\t\v\0");

withoutSpaces

Remove all spaces

$string = 'Hello world';
$string = SM::withoutSpaces($string);
// or
$string = SM::make($string)
    ->withoutSpaces();
echo $string; // Helloworld