如何在PHP中设置文本文件ENCODING(例如UTF-8)?
让我告诉你我的问题.这是我的代码:
<?php
file_put_contents('test.txt', $data); // data is some non-English text with UTF-8 charset
?>
Run Code Online (Sandbox Code Playgroud)
输出:اÙ!
fwrite() 有类似的输出.
但是当我创建test.txtby记事本并设置字符集UTF-8时,输出就是我想要的.我想在PHP文件中设置charset.
现在这是我的问题:如何通过PHP设置文本文件编码?
我们有这样的代码:
echo '<input type="text" name="myInput" value="Double " Quotes" />';
Run Code Online (Sandbox Code Playgroud)
绝对不行,因为之后的报价 Double结束价值.我们可以使用单引号而不是双引号来修复它.
echo '<input type="text" name="myInput" value=\'Double " Quotes\' />';
Run Code Online (Sandbox Code Playgroud)
现在我想使用单引号和双引号作为值.它应该输出She said:"I don't know."
有没有办法在不使用HTML实体(Like ")htmlentities()或类似函数的情况下修复它?
这是test.php文件:
<?php
$string = 'A string with no numbers';
for ($i = 0; $i <= strlen($string)-1; $i++) {
$char = $string[$i];
$message_keyword = in_array($char, range(0,9)) ? 'includes' : 'desn\'t include';
}
// output
echo sprintf('This variable %s number(s)', codeStyle($message_keyword));
// function
function codeStyle($string) {
return '<span style="background-color: #eee; font-weight: bold;">' . $string . '</span>';
}
?>
Run Code Online (Sandbox Code Playgroud)
它按字符分割字符串,并检查字符是否为数字。
问题:其输出始终为 “此变量包括数字”。请帮助我找到原因。提示:当我更改为时range(0,9),range(1,9)它可以正常工作(但无法检测到0)。