Created
September 17, 2022 11:50
-
-
Save tidusvn05/40414d78979f6202d80268886d1fc82a to your computer and use it in GitHub Desktop.
String helpers in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 '); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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