从文本文件调用值并添加到 php var
Call value from text file and add to php var
在下面的代码中,我试图找出如何确定文本文件中一行的值。我不断收到以下错误。我尝试了很多组合来修复但没有运气。我是 php 的新手,所以非常感谢您提供的任何帮助。
错误解决后,我正在寻找它以从文本文件 "defaultcolor" 值中获取颜色代码,并将其更改为与文本框值相等的新颜色。
我正在尝试让 var $currentcolor 报告文本文件中的值。该值为#123456
这是错误
Warning: preg_replace(): No ending delimiter '#' found
这是我的文本文件中的行。
currentcolor = #123456
defaultcolor = #379BB9
这是我的 php
<?php
if(isset($_REQUEST['setcolor'])){
$arr = glob("css\*.css");
$colorcode = $_POST['defaultcolor'];
foreach($arr as $key=>$val){
$str = file_get_contents($val);
$lines_array = file("example.txt");
$search_string = "currentcolor";
foreach($lines_array as $line) {
if(strpos($line, $search_string) !== false) {
list(, $new_str) = explode("=", $line);
}
}
$currentcolor = $new_str;
$str = preg_replace($currentcolor, $colorcode, $str);
file_put_contents($val, $str);
}
}
echo '<label id="steps">Here you can set the theme color.</label>';
echo '<br>';
echo '<Form name="default1" method="POST" action="example.php">';
echo '<label for="defaultcolor">Theme Color: </label><input style="color:#000000" type="text" id="defaultcolor" name="defaultcolor" value="#379BB9">';
echo '<br>';
echo '<input type="submit" name="setcolor" value="Set Theme Color">';
echo '</form>';
echo '<br>';
?>
尝试做一个
die($currentcolor)
在你 运行 之前 preg_replace
。需要用特殊符号换行(如#
)
马特
使用str_replace
(http://php.net/str_replace ) instead of preg_replace
(http://php.net/preg_replace)。
$str = str_replace($currentcolor, $colorcode, $str);
preg_replace
将尝试使用正则表达式来执行替换,但看起来您只是在进行简单的字符串替换。
preg_replace 是正则表达式函数,因此需要分隔符。似乎 PHP 认为您正在尝试使用“#'as a delimiter. So if you got '#123456”,它认为您想要捕获“123456”并试图警告您右侧的“#”丢失。所以正如 Clayton 所说,您可能需要考虑使用字符串替换。
在下面的代码中,我试图找出如何确定文本文件中一行的值。我不断收到以下错误。我尝试了很多组合来修复但没有运气。我是 php 的新手,所以非常感谢您提供的任何帮助。
错误解决后,我正在寻找它以从文本文件 "defaultcolor" 值中获取颜色代码,并将其更改为与文本框值相等的新颜色。
我正在尝试让 var $currentcolor 报告文本文件中的值。该值为#123456
这是错误
Warning: preg_replace(): No ending delimiter '#' found
这是我的文本文件中的行。
currentcolor = #123456
defaultcolor = #379BB9
这是我的 php
<?php
if(isset($_REQUEST['setcolor'])){
$arr = glob("css\*.css");
$colorcode = $_POST['defaultcolor'];
foreach($arr as $key=>$val){
$str = file_get_contents($val);
$lines_array = file("example.txt");
$search_string = "currentcolor";
foreach($lines_array as $line) {
if(strpos($line, $search_string) !== false) {
list(, $new_str) = explode("=", $line);
}
}
$currentcolor = $new_str;
$str = preg_replace($currentcolor, $colorcode, $str);
file_put_contents($val, $str);
}
}
echo '<label id="steps">Here you can set the theme color.</label>';
echo '<br>';
echo '<Form name="default1" method="POST" action="example.php">';
echo '<label for="defaultcolor">Theme Color: </label><input style="color:#000000" type="text" id="defaultcolor" name="defaultcolor" value="#379BB9">';
echo '<br>';
echo '<input type="submit" name="setcolor" value="Set Theme Color">';
echo '</form>';
echo '<br>';
?>
尝试做一个
die($currentcolor)
在你 运行 之前 preg_replace
。需要用特殊符号换行(如#
)
马特
使用str_replace
(http://php.net/str_replace ) instead of preg_replace
(http://php.net/preg_replace)。
$str = str_replace($currentcolor, $colorcode, $str);
preg_replace
将尝试使用正则表达式来执行替换,但看起来您只是在进行简单的字符串替换。
preg_replace 是正则表达式函数,因此需要分隔符。似乎 PHP 认为您正在尝试使用“#'as a delimiter. So if you got '#123456”,它认为您想要捕获“123456”并试图警告您右侧的“#”丢失。所以正如 Clayton 所说,您可能需要考虑使用字符串替换。