tags:

views:

49

answers:

6

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

HelloWorld

=>

Hell****ld
A: 
$string = 'HelloWorld';

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

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

Crozin
+1  A: 

use

substr_replace()

like

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

read more :

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

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

You'll need to use substr_replace().

$str = substr_replace("HelloWorld","****",3,-2);
Kyle Decot
A: 
$str="HelloWorld";
print preg_replace("/^(....)....(.*)/","\\1****\\2",$str);
ghostdog74
+1  A: 
<?php
$var="HelloWorld";
$pattern="/oWor/";
$replace="****";
echo preg_replace($pattern,$replace,$var);
?>
muruga