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

发布于 2024-11-19 16:07:03 字数 1191 浏览 2 评论 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);
}

I'm building a routine that creates a settings.ini file from a database table.

I have a potential problem with the output file when the option value stored in the DB contains line breaks.

For example, like the "test_custom_css" setting below...

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

Is there a PHP filter I can apply to the $mySettings variable in the loop below to remove the line breaks?

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清君侧 2024-11-26 16:07:03
$x = preg_replace('/\r\n|\r|\n\r|\n/m', ' ', $x);

删除所有换行符。

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

removes all line breaks.

め七分饶幸 2024-11-26 16:07:03
<?php
$mySettings = str_replace("\r\n", "", $mySettings);
?>
<?php
$mySettings = str_replace("\r\n", "", $mySettings);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文