开发者

Adding line breaks to output file via 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($mySettingsF开发者_StackOverflow社区ile, $mySettings);
    fclose($mySettingsFile);
}


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";


You can use the platform dependent constant PHP_EOL instead

$mySettings = '[settings]' . PHP_EOL;
// ..
$mySettings .= PHP_EOL;


// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';

put it in double quotes

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜