Reputation: 63686
I want to replace with the 4~8
characters of a string with *
,how to do it?
HelloWorld
=>
Hell****ld
Upvotes: 4
Views: 11534
Reputation: 1389
<?php
$e=str_split("HelloWorld");
$e[3]="*";
$e[4]="*";
$e[5]="*";
echo implode($e);
?>
User can change only the characters that he need.
Upvotes: -1
Reputation: 2122
<?php
$var="HelloWorld";
$pattern="/oWor/";
$replace="****";
echo preg_replace($pattern,$replace,$var);
?>
Upvotes: 0
Reputation: 5345
$var="HelloWorld";
$result=substr_replace($var, '****', 4,4 ) . "<br />\n";
Upvotes: -1
Reputation: 343141
$str="HelloWorld";
print preg_replace("/^(....)....(.*)/","\\1****\\2",$str);
Upvotes: -1
Reputation: 20835
You'll need to use substr_replace().
$str = substr_replace("HelloWorld","****",3,-2);
Upvotes: -1
Reputation: 125644
use
substr_replace()
like
substr_replace($string, '****', 4 , 4);
read more :
http://php.net/manual/en/function.substr-replace.php
Upvotes: 10
Reputation: 44406
$string = 'HelloWorld';
for ($i = 4; $i <= 8; ++$i) {
$string[$i] = '*';
}
But there is many, many more ways to do that.
Upvotes: -1