Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何设置UTF-8字符串中的第n个字符?_Php_String_Character - Fatal编程技术网

Php 如何设置UTF-8字符串中的第n个字符?

Php 如何设置UTF-8字符串中的第n个字符?,php,string,character,Php,String,Character,为了获得第n个字符,您可以编写: mb_substr($s,$n,1); 但是如何设置第n个字符呢?像这样: $s = 'hello'; $n = 2; // first l $r = 'x'; // replace with x echo mb_substr($s, 0, $n) . $r . mb_substr($s, $n + 1); 像这样: $s = 'hello'; $n = 2; // first l $r = 'x'; // replace with x echo mb_su

为了获得第n个字符,您可以编写:

mb_substr($s,$n,1);
但是如何设置第n个字符呢?

像这样:

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

$s = 'hello';
$n = 2; // first l
$r = 'x'; // replace with x
echo mb_substr($s, 0, $n) . $r . mb_substr($s, $n + 1);
啊,手工方式:-)谢谢。现在我想知道是否有可能加快速度(UTF-8处理成本很高,get使用一个呼叫,set使用两个)。啊,手动方式:-)谢谢。现在我想知道是否有可能加快速度(UTF-8处理非常昂贵,get使用一个调用,而set使用两个)。