开发者

How to replace the characters in fixed positions in PHP?

开发者 https://www.devze.com 2022-12-24 06:39 出处:网络
I want to replace with the 4~8 characters of a string with *,how to do it? HelloWorld => 开发者_如何学编程

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

HelloWorld

=>
开发者_如何学编程
Hell****ld


use

substr_replace()

like

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

read more :

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


<?php
$var="HelloWorld";
$pattern="/oWor/";
$replace="****";
echo preg_replace($pattern,$replace,$var);
?>


$string = 'HelloWorld';

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

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


You'll need to use substr_replace().

$str = substr_replace("HelloWorld","****",3,-2);


$str="HelloWorld";
print preg_replace("/^(....)....(.*)/","\\1****\\2",$str);


$var="HelloWorld";
$result=substr_replace($var, '****', 4,4 ) . "<br />\n";


<?php
$e=str_split("HelloWorld");
$e[3]="*";
$e[4]="*";
$e[5]="*";
echo implode($e);
?>

User can change only the characters that he need.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号