Writing a UTF8-encoded file in PHP

I am writing a function to dynamically create my Sitemap and site index.

According to the docs on sitemap.org, the file must be encoded in UTF-8.

My function for writing a file is quite simplistic, something like strings:

function generateFile()
{
  $xml = create_xml();
  $fp = @fopen('sitemap', 'w');
  fwrite($fp, $xml);
  fclose($fp);
}

[Edit - added after comments]

The create_xml () element is simplified, for example:

function create_xml()
{
return '<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
                http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
        <loc>http://example.com/</loc>
        <lastmod>2006-11-18</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
</urlset>';
}

Is there anything I need to do to make sure the file is encoded in UTF-8?

Also, I would like a gzip file, and not leave it uncompressed. I know how to compress a file AFTER I saved it to disk. I want to know if (how?), Can I compress a file BEFORE writing to disk?

+3
3

, , ( create_xml() UTF-8. utf8_encode(). , XML <?xml version="1.0" encoding="UTF-8"?>. fopen 'wb', b . , , -is.

0

PHP script utf-8.

, , , create_xml()

0

If you use only ASCII characters, your file will always be in UTF-8.

0
source

Source: https://habr.com/ru/post/1751743/


All Articles