search

Home  >  Q&A  >  body text

php - How to fix file_put_contents execution failure when it contains & symbol?

The execution is successful like this:
<?php
$filename="test.txt";
file_put_contents($filename, "nihao");
?>

But this will fail:
<?php
$filename="test.txt";
file_put_contents($filename, "ni&hao");
?>

Please give me some advice. There is just an extra and symbol. How should I deal with this?

滿天的星座滿天的星座2748 days ago717

reply all(3)I'll reply

  • 迷茫

    迷茫2017-05-16 13:15:03

    "ni&hao" Add the escape character '&' before special characters or change the double quotes to single quotes.

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:15:03

    Is it possible? I have tested both methods and both are fine.

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:15:03

    <?php
    $filename="test.txt";
    file_put_contents($filename, json_encode("ni&hao"));
    ?>
    

    reply
    0
  • Cancelreply