mb_str_split / mb_strtoupper / mb_strtolower
mb_str_split(string $string, int $length = 1, ?string $encoding = null): array — mb_strtoupper(string $string, ?string $encoding = null): string — mb_strtolower(string $string, ?string $encoding = null): string
Two-track splitting and casing: byte-cutting str_split (tears multibyte chars) vs character-cutting mb_str_split; mb_strtoupper/mb_strtolower map by Unicode properties and are locale-independent — "i" is always "I", while Turkish İ/ı needs hand mapping.
C C23
#include <ctype.h>
#include <stdio.h>
#include <string.h>
/* Decode one UTF-8 code point; returns its byte length (0 at end of input) */
static size_t decode_utf8(const unsigned char *s, size_t n, unsigned *cp) {
if (n == 0) return 0;
if (s[0] < 0x80) { *cp = s[0]; return 1; }
if ((s[0] & 0xE0) == 0xC0 && n >= 2) { *cp = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F); return 2; }
if ((s[0] & 0xF0) == 0xE0 && n >= 3) { *cp = ((s[0] & 0x0F) << 12) | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F); return 3; }
if ((s[0] & 0xF8) == 0xF0 && n >= 4) { *cp = ((s[0] & 0x07) << 18) | ((s[1] & 0x3F) << 12) | ((s[2] & 0x3F) << 6) | (s[3] & 0x3F); return 4; }
*cp = 0xFFFD; return 1;
}
int main(void) {
/* C has no multibyte string split: walk UTF-8 manually and cut per character */
const char *s = "\xE4\xBD\xA0\xE5\xA5\xBD" "PHP"; /* CJK chars are 3 bytes each in UTF-8 */
size_t n = strlen(s), i = 0;
while (i < n) {
unsigned cp;
size_t len = decode_utf8((const unsigned char *)s + i, n - i, &cp);
printf("[%.*s]", (int)len, s + i); /* one character = 1..4 bytes */
i += len;
}
printf("\n");
/* toupper() is ASCII-only (and locale-dependent): bytes >= 0x80 pass through */
const char *t = "hello \xC3\xA9"; /* hello é */
for (const char *p = t; *p; p++) {
unsigned char ch = (unsigned char)*p;
putchar(ch < 0x80 ? toupper(ch) : ch);
}
printf("\n");
return 0;
}
[你][好][P][H][P] HELLO é
C++ C++26
#include <cctype>
#include <cstdio>
#include <string>
int main() {
// C++ has no multibyte string split: walk UTF-8 manually, cut per character
const std::string s = "\xE4\xBD\xA0\xE5\xA5\xBD" "PHP"; // the UTF-8 bytes of two CJK chars + PHP
for (size_t i = 0; i < s.size(); ) {
unsigned char lead = s[i];
size_t len = lead < 0x80 ? 1 : (lead & 0xE0) == 0xC0 ? 2 : (lead & 0xF0) == 0xE0 ? 3 : 4;
std::printf("[%.*s]", (int)len, s.data() + i); // one character = 1..4 bytes
i += len;
}
std::printf("\n");
// toupper() is ASCII-only (and locale-dependent): bytes >= 0x80 pass through
const std::string t = "hello \xC3\xA9"; // hello é
for (unsigned char ch : t) {
std::putchar(ch < 0x80 ? std::toupper(ch) : ch);
}
std::printf("\n");
return 0;
}
[你][好][P][H][P] HELLO é
C# v11.0.100
using System.Globalization;
// Length/ToCharArray are UTF-16 code units (emoji need 2); splitting text uses StringInfo
Console.WriteLine("你好PHP".Length);
// ToUpperInvariant = locale-independent (the mbstring behaviour); tr-TR maps i -> İ
Console.WriteLine("hello world".ToUpperInvariant());
Console.WriteLine(char.ToUpperInvariant('i') + " " + char.ToUpper('i', new CultureInfo("tr-TR")));
Console.WriteLine(char.ToLowerInvariant('I') + " " + char.ToLower('I', new CultureInfo("tr-TR")));
5 HELLO WORLD I İ i ı
Go v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
// []rune decodes UTF-8 into code points — character level, like mb_str_split
runes := []rune("你好PHP")
parts := make([]string, len(runes))
for i, r := range runes {
parts[i] = string(r)
}
fmt.Println(parts)
// strings.ToUpper is Unicode-aware and locale-INDEPENDENT (like mbstring)
fmt.Println(strings.ToUpper("hello world"))
// Turkish needs golang.org/x/text/cases (see notes) — hand-map here
fmt.Println(strings.ReplaceAll(strings.ToUpper("izmir"), "I", "İ"))
}
[你 好 P H P] HELLO WORLD İZMİR
Java v27.0.1
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// length() is UTF-16 units; codePointCount is the character count (like mb_strlen)
String s = "你好PHP";
System.out.println(s.codePointCount(0, s.length()));
// toUpperCase() follows the DEFAULT locale — pin Locale.ROOT for the
// locale-independent mapping (the mbstring behaviour), or pass "tr"
System.out.println("hello world".toUpperCase(Locale.ROOT));
System.out.println("i".toUpperCase(Locale.ROOT) + " " + "i".toUpperCase(Locale.forLanguageTag("tr")));
System.out.println("I".toLowerCase(Locale.ROOT) + " " + "I".toLowerCase(Locale.forLanguageTag("tr")));
}
}
5 HELLO WORLD I İ i ı
JavaScript v26.13.1
// Splitting is code-point level: [...s] / Array.from keep surrogate pairs whole
console.log([..."你好PHP"]);
console.log("hello world".toUpperCase());
// Case mapping: toUpperCase() is locale-INDEPENDENT; toLocaleUpperCase() takes a locale
console.log("i".toUpperCase(), "i".toLocaleUpperCase("tr"));
console.log("I".toLowerCase(), "I".toLocaleLowerCase("tr"));
[ '你', '好', 'P', 'H', 'P' ] HELLO WORLD I İ i ı
PHP v8.6.0
// Track 1: splitting — str_split cuts BYTES (tears multibyte chars), mb_str_split cuts CHARACTERS
var_export(mb_str_split("你好PHP", 1, "UTF-8"));
echo "\n";
var_export(mb_str_split("你好PHP", 2, "UTF-8"));
echo "\n";
// Track 2: case mapping — Unicode-aware and locale-INDEPENDENT (per the manual)
echo mb_strtoupper("Hello World", "UTF-8"), "\n";
echo mb_strtolower("CAFÉ ÉÉ", "UTF-8"), "\n";
echo mb_strtoupper("i"), " ", mb_strtolower("I"), "\n"; // always plain I / i — no locale variants
echo mb_convert_case("hello world-foo", MB_CASE_TITLE, "UTF-8"), "\n";
array ( 0 => '你', 1 => '好', 2 => 'P', 3 => 'H', 4 => 'P', ) array ( 0 => '你好', 1 => 'PH', 2 => 'P', ) HELLO WORLD café éé I i Hello World-Foo
Python v3.15.2
# Splitting is character level: Python strings are code-point sequences
print(list("你好PHP"))
# Case mapping is Unicode-aware and locale-INDEPENDENT (like PHP mbstring)
print("hello world".upper(), "CAFÉ ÉÉ".lower())
print("i".upper(), "I".lower()) # always plain I / i — no locale variants
# Turkish needs a hand-rolled mapping (the PHP manual note's approach)
print("izmir".upper().replace("I", "İ"))
['你', '好', 'P', 'H', 'P'] HELLO WORLD café éé I i İZMİR
Rust v1.99.1
fn main() {
// chars() walks Unicode scalar values (code points) — char count, not bytes
let v: Vec<char> = "你好PHP".chars().collect();
println!("{:?}", v);
// to_uppercase() is full Unicode case mapping, locale-INDEPENDENT (like mbstring)
println!("{}", "hello world".to_uppercase());
println!("{} {}", "i".to_uppercase(), "I".to_lowercase()); // always plain I / i
// Turkish has no built-in switch — hand-map like the PHP manual note does
let tr_upper = "izmir".to_uppercase().replace('I', "İ");
println!("{}", tr_upper);
}
['你', '好', 'P', 'H', 'P'] HELLO WORLD I i İZMİR
TypeScript v26.13.1
// Splitting is code-point level: [...s] / Array.from keep surrogate pairs whole
console.log([..."你好PHP"]);
console.log("hello world".toUpperCase());
// Case mapping: toUpperCase() is locale-INDEPENDENT; toLocaleUpperCase() takes a locale
console.log("i".toUpperCase(), "i".toLocaleUpperCase("tr"));
console.log("I".toLowerCase(), "I".toLocaleLowerCase("tr"));
[ '你', '好', 'P', 'H', 'P' ] HELLO WORLD I İ i ı
Language differences
PHP: mb_str_split is the multibyte str_split — str_split cuts bytes (a Chinese character is torn in half) while mb_str_split cuts characters and its length counts characters ("你好PHP" with length 2 gives ["你好","PH","P"]); mb_strtoupper/mb_strtolower map by Unicode properties, and the manual states the behaviour "is not affected by locale settings" — "i" is always "I"; mb_convert_case(..., MB_CASE_TITLE) adds title casing. Turkish measured on this machine: mb_strtoupper('i', 'tr') throws ValueError under PHP 8.6.0 — the second parameter is an encoding, not a locale (the core teaching contrast with Java/C#/JS).
C: no multibyte split/case — the example walks UTF-8 manually to cut per character ([你][好][P][H][P]); toupper is ASCII-only and locale-dependent, so é passes through untouched (the HELLO é control line).
C++: same as C; use ICU or a cross-platform library for Unicode casing in production.
C#: .Length/ToCharArray are UTF-16 code units (StringInfo is needed for text splitting); char.ToUpperInvariant is the locale-independent version (the mbstring behaviour), while char.ToUpper(c, new CultureInfo("tr-TR")) yields İ — locale-aware casing is a first-class citizen in .NET.
Go: []rune decodes UTF-8 into code points, i.e. character-level splitting (like mb_str_split); strings.ToUpper is Unicode-aware and locale-independent; Turkish lives in golang.org/x/text/cases (the example hand-rolls ReplaceAll to show the semantics).
Java: codePointCount is the character count (like mb_strlen); toUpperCase() without arguments follows the DEFAULT locale — pin Locale.ROOT for the mbstring-style locale-independent mapping, and Locale.forLanguageTag("tr") yields İ (the example contrasts I İ / i ı across two lines).
JavaScript: [...s] splits at code-point level (surrogate pairs stay whole); toUpperCase() is locale-independent, toLocaleUpperCase("tr") yields İ.
Python: list(s) is natively character-level; str.upper() is full Unicode character-level casing, also locale-independent — the same camp as mbstring; Turkish is hand-rolled as "izmir".upper().replace("I","İ") = İZMİR (mirroring the PHP manual note).
Rust: chars().collect() is character splitting; to_uppercase() is full Unicode mapping (ß→SS etc.) and locale-independent; Turkish is a hand-written replace.
TypeScript: same as JavaScript.
Determinism: all split/case inputs are fixed; the İ (U+0130) / ı (U+0131) mappings are identical on every platform and version; the demo deliberately avoids conditional mappings (Greek final σ/ς) and length-changing characters (ß→SS is full case mapping — measured on this machine, PHP 8.6.0 mb_strtoupper('straße') yields 'STRASSE' while earlier versions yield 'STRAßE', a cross-version difference, so it stays out of the demo). Conditional mappings are officially unimplemented in mbstring (php-src issue #8096, bug #70072).