Skip to content

Instantly share code, notes, and snippets.

@tidusvn05
Created September 17, 2022 11:50
Show Gist options
  • Save tidusvn05/40414d78979f6202d80268886d1fc82a to your computer and use it in GitHub Desktop.
Save tidusvn05/40414d78979f6202d80268886d1fc82a to your computer and use it in GitHub Desktop.
String helpers in PHP
<?php
/**
* Uppercase to lowercase and lowercase to uppercase(ASCII string only).
*
* @param string $value
* @return string
*/
function invertCase($value)
{
return strtolower($value) ^ strtoupper($value) ^ $value;
}
// echo invertCase("Invert My CASE");
// => iNVERT mY case
<?php
/**
* Right trim string.
*
* @param string $value
* @param bool $fullsize
* @return string
*/
// function rtrim($value, $fullsize = true)
// {
// if ($fullsize) {
// $value = preg_replace('/[\s]+$/u', '', $value);
// }
// return $value;
// }
echo 'aaaa ';
echo rtrim('aaaa ');
echo rtrim('aaaa ');
<?php
/**
* Convert a value to studly caps case.
*
*
* @param string $value
* @return string StudlyCaseString
*/
function studlyCase($value)
{
$words = explode(' ', str_replace(['-', '_'], ' ', $value));
$studlyWords = array_map(function ($word) {
return ucfirst($word);
}, $words);
return implode($studlyWords);
}
// echo studlyCase("studly Case string");
// => StudlyCaseString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment