Php jquery$.get()添加斜杠即使删除斜杠也不保存编辑
我在wordpress插件中创建了一个简单的css编辑/优化系统。基本上,css文件存储在一个文件夹中。可以使用jquery通过选择框访问它们进行编辑 HTMLPhp jquery$.get()添加斜杠即使删除斜杠也不保存编辑,php,jquery,Php,Jquery,我在wordpress插件中创建了一个简单的css编辑/优化系统。基本上,css文件存储在一个文件夹中。可以使用jquery通过选择框访问它们进行编辑 HTML # edit stylesheet echo '<div id="editStyles" class="s8w-hide">'; echo '<h2>Edit a Stylesheet</h2>';
# edit stylesheet
echo '<div id="editStyles" class="s8w-hide">';
echo '<h2>Edit a Stylesheet</h2>';
echo '<form action="" method="post">';
# file name
echo '<div class="s8w-row">';
echo '<div class="s8w-col_12">';
echo '<select id="getStylesheeteEdit" name="filename" class="s8w-expand">';
echo '<option value="">Select Stylesheet to Edit</option>';
foreach(glob(S8W_CSS_GEN . 'common/*.css') as $file) {
$filename = basename($file);
echo '<option value="'.$filename.'">'.$filename.'</option>';
}
echo '</select>';
echo '</div>';
echo '</div>';
# styles
echo '<div class="s8w-row">';
echo '<div class="s8w-col_12">';
echo '<textarea id="showEditStyles" name="styles" class="s8w-expand" style="height: 350px;"></textarea>';
echo '</div>';
echo '</div>';
echo '<div class="s8w-row">';
echo '<div class="s8w-col_12 s8w-center">';
echo '<input type="submit" name="edit_stylesheet" class="s8w navy tiny-radius" value="Edit this Stylesheet">';
echo '</div>';
echo '</div>';
echo '</form>';
解析php
我在这里添加了斜线,删除了斜线
/* EDIT STYLESHEET
--------------------------------------------------------- */
if(array_key_exists('edit_stylesheet',$_POST)){
$filename = $_POST['filename'];
$styles = stripslashes($_POST['styles']);
# update styles
file_put_contents(S8W_CSS_GEN . 'common/'.$filename, $styles);
# parse stylesheets
$msg = 'The stylesheet: '.$filename.' and the S8W Stylesheet have been updated.';
parseStyles($msg);
}
parseStyles($msg)最小化并写入单个样式表
<?php
function parseStyles($msg){
/* admin styles
-------------------------------------- */
foreach(glob(S8W_CSS_GEN . 'admin/*.css') as $file) {
$admin_code .= file_get_contents($file);
$prep_styles = file_get_contents($file);
$prep_styles = str_replace("/*","~",$prep_styles);
$prep_styles = str_replace("*/","~",$prep_styles);
preg_match_all("'~(.*?)~'si", $prep_styles, $match);
foreach($match[1] as $val) {
$this_comment = '~'.$val.'~';
$prep_styles = str_replace($this_comment,"",$prep_styles);
}
//$prep_styles = str_replace('\"','"',$prep_styles);
$prep_styles = str_replace("\r","",$prep_styles);
$prep_styles = str_replace("\n","",$prep_styles);
$prep_styles = str_replace("\t","",$prep_styles);
$admin_styles .= $prep_styles;
}
/* common styles
-------------------------------------- */
foreach(glob(S8W_CSS_GEN . 'common/*.css') as $file) {
$common_code .= file_get_contents($file);
$prep_styles = file_get_contents($file);
$prep_styles = str_replace("/*","~",$prep_styles);
$prep_styles = str_replace("*/","~",$prep_styles);
preg_match_all("'~(.*?)~'si", $prep_styles, $match);
foreach($match[1] as $val) {
$this_comment = '~'.$val.'~';
$prep_styles = str_replace($this_comment,"",$prep_styles);
}
//$prep_styles = str_replace('\"','"',$prep_styles);
$prep_styles = str_replace("\r","",$prep_styles);
$prep_styles = str_replace("\n","",$prep_styles);
$prep_styles = str_replace("\t","",$prep_styles);
$common_styles .= $prep_styles;
}
# write admin styles
$admin_styles = $common_styles.$admin_styles;
file_put_contents(S8W_CSS . 's8w-admin-styles.css', $admin_styles);
# write admin styles
$admin_readable = $common_code.$admin_code;
file_put_contents(S8W_CSS_GEN . 'readable-admin.css', $admin_readable);
$common_styles = $common_styles;
# write admin styles
file_put_contents(S8W_CSS . 's8w-styles.css', $common_styles);
# write admin styles
$common_readable = $common_code;
file_put_contents(S8W_CSS_GEN . 'readable.css', $common_readable);
echo '<Script language="javascript">alert("'.$msg.'");</script>';
}
文件将保存并最小化。但是,当通过jquery调用该文件时,尽管编辑已保存到该文件中,但新编辑不会在文本区域中呈现,因此,如果再次将该文件保存到BCE,则会丢失。希望我能把问题解释清楚。非常感谢您的帮助或想法。虽然我仍然不确定发生了什么,但问题出在这个jQuery中:
/* GET STYLESHEET TO EDIT
----------------------------------------------------- */
$( "#getStylesheeteEdit" ).change(function() {
var which = $(this).val();
var file = 'https://s8w.org/wp-content/plugins/IDCCST/css/generator/common/' + which;
$.get(file, function(data) {
//data.replace('\\','');
$('#showEditStyles').val(data);
});
});
当一个css文件被编辑时,编辑内容将被保存并通过minimizer php解析,但是当被重新选择时,上面的jQuery将不会对保存的文件进行编辑。不知道为什么
通过将jQuery更改为:
$( "#getStylesheeteEdit" ).change(function() {
var which = $(this).val();
var url = 'https://s8w.org/wp-content/plugins/IDCCST/css/generator/get-stylsheet.php';
var postit = $.post( url, {which:which});
postit.done(function( data ) {$('#showEditStyles').val(data);});
});
并添加一个只包含一行代码的php解析文件,以便能够使用file\u get\u contents()
,而不是使用jQuery获取所需css文件的内容:
<?php
$file = 'common/'.$_POST['which']; echo file_get_contents($file);
jQuery不应该对斜杠做任何事情。尝试添加内容类型'html'
作为$.get(函数(数据){/*code*/},'html')
的第二个参数。这是通过一个php输出缓冲区进行的,该缓冲区在输出前可能会进行一些转义吗?php不会向输出添加斜杠。还要注意的是,字符串#replace()仅在带有g标志的正则表达式不存在的情况下在第一个实例上有效used@charlietfl我尝试了$.get(函数(数据){/*code*/},'html')
,但该文件没有被调用到文本区域进行编辑。我不知道你所说的输出缓冲区是什么意思。在选择getStylesheeteEdit
时,jQuery会像php中的file\u get\u contents()
一样获取文件。如果返回包含双引号,则在此处添加斜杠。如果文件没有引号,则所有内容都有效。编辑将被保存,如果再次选择相同的编辑文件,编辑将按预期显示在文本区域中。使用引号保存编辑,但如果再次选中,编辑不会在文本区域中呈现,因此请尝试使用条带斜杠
$( "#getStylesheeteEdit" ).change(function() {
var which = $(this).val();
var url = 'https://s8w.org/wp-content/plugins/IDCCST/css/generator/get-stylsheet.php';
var postit = $.post( url, {which:which});
postit.done(function( data ) {$('#showEditStyles').val(data);});
});
<?php
$file = 'common/'.$_POST['which']; echo file_get_contents($file);