2011-07-13 56 views
0

我正在構建一個例程,它可以從數據庫表中創建一個settings.ini文件。從數據庫字段值中刪除換行符

當存儲在數據庫中的選項值包含換行符時,輸出文件存在潛在的問題。

例如,像「test_custom_css」下面設置...

test_current_sort = asc 
test_custom_css = /*.blog ul li {margin-bottom:0;padding-bottom:0;} 

#facebook_like iframe {height:30px;} 

.footer ul li:last-child a {border:none;} 
*/ 
test_custom_footer = true 
test_custom_header = true 
test_friendlyURLS = 1 

有以下PHP過濾器,我可以適用於$ mySettings變量循環刪除換行符?

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; //REMOVE LINE BREAKS HERE 
     $mySettings .= '\r\n'; 
    } 

    $mySettingsFileLocation = WP_PLUGIN_DIR.'/test/settings-backup.ini'; 
    $mySettingsFile = fopen($mySettingsFileLocation, 'w'); 
    fwrite($mySettingsFile, $mySettings); 
    fclose($mySettingsFile); 
} 

回答

0
$x = preg_replace('/\r\n|\r|\n\r|\n/m', ' ', $x); 

刪除所有換行符。

0
<?php 
$mySettings = str_replace("\r\n", "", $mySettings); 
?>