PHP

UTF-8 Safe substr / strlen

admin by @admin ADMIN
Jun 15, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Wrap mb_* with sensible defaults so you stop accidentally chopping multi-byte characters in half. Defensive helpers for projects where input is not guaranteed ASCII.
PHP
Raw
<?php
function strLenU(string $s): int {
    return mb_strlen($s, 'UTF-8');
}

function subStrU(string $s, int $start, ?int $len = null): string {
    return $len === null
        ? mb_substr($s, $start, null, 'UTF-8')
        : mb_substr($s, $start, $len, 'UTF-8');
}

function strPosU(string $haystack, string $needle, int $offset = 0): int|false {
    return mb_strpos($haystack, $needle, $offset, 'UTF-8');
}

// Strict UTF-8 → fixed string, or false on invalid bytes.
function ensureUtf8(string $s): string|false {
    return mb_check_encoding($s, 'UTF-8') ? $s : false;
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.