Saturday, 31 August 2013

Function not acting the same way when run twice

Function not acting the same way when run twice

I made a function that basically separates out some php variables on a
document then pieces them back together after altering one.
Runs fine if I call the function once but when I run it twice I cuts off
some of the last segment. It also isn't the same amount cut off each time.
I was wondering if there was any reason for this.
function editvar($filename, $old_var, $new_var)
{
$tmp = fopen($filename, "r");
$content=fread($tmp,filesize($filename));
fclose($tmp);
$ex = explode('<!---->', $content);
$expl = $ex[0];
$expl = explode('"', $ex[0]);
$expl[$old_var] = $new_var; // changed selected variable.
$ii = 0;
$ex[0] = "";
foreach($expl as $value) {
$ex[0] .= $expl[$ii];
if ($ii < count($expl) - 1)
{
$ex[0] .='"';
}
$ii++;
}
$ii = 0;
$new_content = "";
foreach($expl as $value)
{
$new_content .= $ex[$ii];
if ($ii < count($ex) - 1)
{
$new_content .= '<!---->';
}
$ii++;
}
$tmp = fopen($filename, "w");
fwrite($tmp, $new_content);
fclose($tmp);
}

No comments:

Post a Comment