I want to replace with the 4~8
characters of a string with *
,how to do it?
HelloWorld
=>
Hell****ld
I want to replace with the 4~8
characters of a string with *
,how to do it?
HelloWorld
=>
Hell****ld
$string = 'HelloWorld';
for ($i = 4; $i <= 8; ++$i) {
$string[$i] = '*';
}
But there is many, many more ways to do that.
use
substr_replace()
like
substr_replace($string, '****', 4 , 4);
read more :
$var="HelloWorld";
$result=substr_replace($var, '****', 4,4 ) . "<br />\n";
You'll need to use substr_replace().
$str = substr_replace("HelloWorld","****",3,-2);
$str="HelloWorld";
print preg_replace("/^(....)....(.*)/","\\1****\\2",$str);
<?php
$var="HelloWorld";
$pattern="/oWor/";
$replace="****";
echo preg_replace($pattern,$replace,$var);
?>