我正在尝试格式化我正在创建的文件,以便每个名称/值对都在其自己的行上
我确信这很简单,但我的.ini文件根本没有格式化换行符。我错过了什么?
function wpseTest()
{
$query = "SELECT option_name, option_value FROM wp_options where option_name like 'test|_%' escape '|' AND option_value > ''";
global $wpdb;
$matches = $wpdb->get_results($query);
$mySettings = '[settings]\r\n';
foreach ($matches as $result){
$mySettings .= $result->option_name;
$mySettings .= ' = ';
$mySettings .= $result->option_value;
$mySettings .= '\r\n';
}
$mySettingsFileLocation = WP_PLUGIN_DIR.'/test/settings-backup.ini';
$mySettingsFile = fopen($mySettingsFileLocation, 'w');
fwrite($mySettingsFile, $mySettings);
fclose($mySettingsFile);
}
答案 0 :(得分:18)
\r
和\n
等特殊字符不会在单引号中解释。改为使用双引号。
$mySettings = "[settings]\r\n";
和
$mySettings .= "\r\n";
答案 1 :(得分:2)
您可以使用平台相关常量PHP_EOL
代替
$mySettings = '[settings]' . PHP_EOL;
// ..
$mySettings .= PHP_EOL;
答案 2 :(得分:0)
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
function wpseTest()
{
$query = "SELECT option_name, option_value FROM wp_options where option_name like 'test|_%' escape '|' AND option_value > ''";
global $wpdb;
$matches = $wpdb->get_results($query);
$mySettings = "[settings]\r\n";
foreach ($matches as $result){
$mySettings .= $result->option_name;
$mySettings .= ' = ';
$mySettings .= $result->option_value;
$mySettings .= "\r\n";
}
$mySettingsFileLocation = WP_PLUGIN_DIR.'/test/settings-backup.ini';
$mySettingsFile = fopen($mySettingsFileLocation, 'w');
fwrite($mySettingsFile, $mySettings);
fclose($mySettingsFile);
}