How to replace the characters in fixed positions in PHP?
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.
精彩评论