从数据库字段值中删除换行符

时间:2011-07-13 18:32:26

标签: php string-formatting sanitization

我正在构建一个例程,用于从数据库表中创建 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);
}

2 个答案:

答案 0 :(得分:1)

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

删除所有换行符。

答案 1 :(得分:0)

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