greenoldman
greenoldman

Reputation: 21062

How to set n-th character in UTF-8 string?

In order to get n-th character you can write:

mb_substr($s,$n,1);

but how to set n-th character?

Upvotes: -1

Views: 60

Answers (1)

user128161
user128161

Reputation:

Like so:

$s = 'hello';
$n = 2; // first l
$r = 'x'; // replace with x
echo mb_substr($s, 0, $n) . $r . mb_substr($s, $n + 1);

Upvotes: 2

Related Questions