Adding line breaks to output file via fwrite

Scott B picture Scott B · Jul 13, 2011 · Viewed 21.1k times · Source

I'm trying to format the file I'm creating below so that each name/value pair is on its own line

I'm sure this is easy, but my .ini file is not formatting the line breaks at all. what am I missing?

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);
}

Answer

user703016 picture user703016 · Jul 13, 2011

Special characters like \r and \n do not get interpreted in single quotes. Use double quotes instead.

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

And

$mySettings .= "\r\n";