James30263 James30263 - 1 month ago 11
HTML Question

Call value from text file and add to php var

On the following code I'm trying to find out how to determine the value of a line in a text file. I keep getting the error below. I tried many combinations to fix but no luck. I'm new to php so I appreciate any help given.

Once I get the error resolved I'm looking for it to take the color code from the text file "defaultcolor" value and change it to the new color that equals the text box value.

I'm trying to get var $currentcolor to report back the value from the text file. That value is #123456

Here's the error

Warning: preg_replace(): No ending delimiter '#' found


Here's the lines in my text file.

currentcolor = #123456
defaultcolor = #379BB9


Here's my 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>';
?>




Answer

Use str_replace (http://php.net/str_replace ) instead of preg_replace (http://php.net/preg_replace).

$str = str_replace($currentcolor, $colorcode, $str); 

preg_replace will attempt to use regular expression to perform the substitution but it appears you are only doing a simple string substitution.

Comments