How to replace the characters in fixed positions i

2020-02-06 08:23发布

I want to replace with the 4~8 characters of a string with *,how to do it?

HelloWorld

=>

Hell****ld

标签: php string
7条回答
ゆ 、 Hurt°
2楼-- · 2020-02-06 08:52
<?php
$e=str_split("HelloWorld");
$e[3]="*";
$e[4]="*";
$e[5]="*";
echo implode($e);
?>

User can change only the characters that he need.

查看更多
太酷不给撩
3楼-- · 2020-02-06 08:53
$str="HelloWorld";
print preg_replace("/^(....)....(.*)/","\\1****\\2",$str);
查看更多
Emotional °昔
4楼-- · 2020-02-06 08:57
$string = 'HelloWorld';

for ($i = 4; $i <= 8; ++$i) {
    $string[$i] = '*';
}

But there is many, many more ways to do that.

查看更多
爷、活的狠高调
5楼-- · 2020-02-06 09:05

You'll need to use substr_replace().

$str = substr_replace("HelloWorld","****",3,-2);
查看更多
Luminary・发光体
6楼-- · 2020-02-06 09:09
$var="HelloWorld";
$result=substr_replace($var, '****', 4,4 ) . "<br />\n";
查看更多
混吃等死
7楼-- · 2020-02-06 09:15

use

substr_replace()

like

substr_replace($string, '****', 4 , 4);

read more :

http://php.net/manual/en/function.substr-replace.php

查看更多
登录 后发表回答