0

Currently I have an XML file

<?xml version="1.0" encoding="UTF-8"?>
<Workflow xmlns="http://ns.adobe.com/acrobat/workflow/2012" title="HongKong2_Convert_img" description="" majorVersion="1" minorVersion="0">
<Sources>&lt;Folder path="/C/Source/HongKong2/temp"/&gt;</Sources>
</Workflow>

What I would like to do is to replace &lt; with < and &gt; with > .

$handle = fopen('a.xml', "r+") or die();

while(!feof($handle)) {
    if (strstr(fgets($handle),"&lt;") || strstr(fgets($handle),"&gt;")) {
        echo 'tttt';
        fwrite($handle, str_replace("&lt;", "<", fgets($handle))); 
        fwrite($handle, str_replace("&gt;", ">", fgets($handle))); 
    }
}

fclose($handle);

I can trigger the if condition and it should be correct. The only problem is it can not replace that line (some problem with fwrite..) . How to fix that? thanks

user1871516
  • 979
  • 6
  • 14
  • 26

2 Answers2

1

You are calling fgets multiple times and doing nothing with the results. Each time you call fgets it reads from the file and advances the file pointer. Move the fgets outside of your loop assign the value to a variable and test against the variable

Orangepill
  • 24,500
  • 3
  • 42
  • 63
1
$handle = fopen('a.xml', "r+") or die();
$line = fgets($handle);
while($line) {
    if (strstr($line,"&lt;") || strstr($line,"&gt;")) {
        echo 'tttt';
        $newline = str_replace("&lt;", "<", $line);
        $newline = str_replace("&gt;", ">", $newline);
        fwrite($handle, $newline); 
    }
    $line = fgets($handle);
}

fclose($handle);
Qullbrune
  • 1,925
  • 2
  • 20
  • 20
isaach1000
  • 1,819
  • 1
  • 13
  • 18