从数据库字段值中删除换行符
我正在构建一个从数据库表创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除所有换行符。
removes all line breaks.