2011-07-13 50 views
1

我試圖格式化,我下面創建使每一個名稱/值對自身行的文件,以打破輸出文件添加線過FWRITE

我敢肯定,這是很容易的,但我.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); 
} 

回答

9

特殊字符像\r\n沒有得到解釋的單引號。改用雙引號。

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

而且

$mySettings .= "\r\n"; 
0
// 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); 
} 
2

您可以使用該平臺相關的常數代替PHP_EOL

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