i3ry Gee
i3ry Gee

Reputation: 33

str_replace content using custom function

I am using Method One to string replace values and works fine. I have now got the hand of custom functions so I want to change this into an easier custom function method as in Method Two but it does not work. Any help greatly appreciated.

$plarge = $_POST['plarge'];
$phmaxl = $_POST['phmaxl'];
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));

#### METHOD ONE ######
$content = str_replace($large,$plarge,$content);
$content = str_replace($hmaxl,$phmaxl,$content);
######################

#### METHOD TWO ######
function setreplace($set){
$content = str_replace($set,p.$set,$content);
}
setreplace($phmaxl);
setreplace($plarge);
#######################


####### WRITES  NEW VAULUES TO SETTINGS FILE ############
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
####### CLOSES SETTINGS FILE ############
fclose($fhandle); 

Upvotes: 0

Views: 335

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

Functions don't access the outside scope, and you can't concatenate variable names like that.

Consider doing something like this instead:

$replacements = array();
$replacements[$large] = $plarge;
$replacements[$hmaxl] = $phmaxl;
$content = strtr($content,$replacements);

strtr()

Upvotes: 1

Related Questions