Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding line breaks to output file via fwrite

Tags:

php

fwrite

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);
}
like image 245
Scott B Avatar asked Nov 28 '22 10:11

Scott B


1 Answers

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";
like image 161
user703016 Avatar answered Dec 19 '22 05:12

user703016