How to convert a string to uppercase or lowercase
Short answer
Use the functions with the mb_ prefix — they work with any alphabet:
<?php
echo mb_strtoupper('привет, world'), "\n";
echo mb_strtolower('ПРИВЕТ, World');Output:
ПРИВЕТ, WORLD
привет, worldWhy not strtoupper()
The plain functions change ASCII letters only:
<?php
echo strtoupper('привет, world');Output:
привет, WORLDA capital first letter
For non-ASCII text the first letter is converted separately:
<?php
$text = 'привет';
echo mb_strtoupper(mb_substr($text, 0, 1)) . mb_substr($text, 1);Output:
ПриветFunctions used in this recipe
- mb_strtoupper — Converts a string to uppercase.
- mb_strtolower — Converts a string to lowercase for any alphabet.
- strtoupper — Converts ASCII letters to uppercase.