我正在尝试格式化我在下面创建的文件,以便每个名称/值对都在自己的行上

我确信这很容易,但我的 .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);
}

最佳答案

\r\n 等特殊字符不会用单引号解释。请改用双引号。

$mySettings = "[settings]\r\n";


$mySettings .= "\r\n";

关于php - 通过 fwrite 向输出文件添加换行符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6683595/

10-09 18:14