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

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.