好的,我正在向我在下面的函数中读取的 UTF-8 文件写入注释,以删除这些注释之间的文本。我的问题是,我需要什么不同的东西来为 UTF-8 文件成功地做到这一点吗?或者下面的代码会起作用吗?基本上,我想知道我是否需要utf8_decode
和/或utf8_encode
功能,或者iconv
功能?
// This holds the current file we are working on.
$lang_file = 'files/DreamTemplates.russian-utf8.php';
// Can't read from the file if it doesn't exist now can we?
if (!file_exists($lang_file))
continue;
// This helps to remove the language strings for the template, since the comment is unique
$template_begin_comment = '// ' . ' Template - ' . $lang_file . ' BEGIN...';
$template_end_comment = '// ' . ' Template - ' . $lang_file . ' END!';
$fp = fopen($lang_file, 'rb');
$content = fread($fp, filesize($lang_file));
fclose($fp);
// Searching within the string, extracting only what we need.
$start = strpos($content, $template_begin_comment);
$end = strpos($content, $template_end_comment);
// We can't do this unless both are found.
if ($start !== false && $end !== false)
{
$begin = substr($content, 0, $start);
$finish = substr($content, $end + strlen($template_end_comment));
$new_content = $begin . $finish;
// Write it into the file.
$fo = fopen($lang_file, 'wb');
@fwrite($fo, $new_content);
fclose($fo);
}
感谢您在有关字符串的 UTF-8 编码和解码方面的帮助,即使它们是注释字符串。
当我将 php 注释写入 UTF-8 文件时,我没有使用任何转换。我可以做??php 注释之间的字符串定义已经以 UTF-8 编码,但是在文件中似乎可以正常工作。任何帮助在这里表示赞赏。