Page 1 of 1

[Solved] Backslashing problem

Posted: Mon Sep 26, 2005 2:33 pm
by pilau
Hi all.
In my code I am reading strings from a JS array which is found in a file.
Some of the strings have esacped single-quotes in them, for example: "New Year's Eve".

In my code, I am reading them, then printing them out to an HTML form.
When the form is submitted I am retrieving them back by POST, and then writing the file again.

However, when I load the file the next time, what I get is "New Year\" and that's it.
What could cause that?

Posted: Mon Sep 26, 2005 11:43 pm
by ruchit
it would be better if you can post some of your code... meanwhile you can try out stripslashes()... b'cos it seems to be an issue with the magic quotes more than anything else

Posted: Fri Sep 30, 2005 12:25 am
by pilau
Magic quotes are off, but it's not working.

Here's the code the reads the file and prints the page:

Code: Select all

function read_contents() {
	$events_script = file_get_contents("events.script");
		$file = preg_split("/events\[[0-9]{1,2}\]=\{/", $events_script);
	  array_shift($file);
	return $file;
 } // read end

function print_page($months) {

   //Begin Master array interpretation
	while (list($var, $val) = each($months)) {
		preg_match_all("#^\s*(\d+):\"(.*?)\".*$#m", $months[$var], $events);
		array_shift($events);
		print " <tr bgcolor=\"#FFCC00\">
          <td colspan=\"2\">
          	<input type=\"checkbox\" onClick=\"getStatus('".return_month_name($var)."')\" checked>
            " . return_month_name($var) . " <font size=\"1\">(Uncheck to remove events)</font>
          </td>
        </tr>
";

   //Begin Secondary array interpretation
		$events = array_combine_emulated($events[0],$events[1]);
	$i = 0;
	while (list($event_key, $event_val) = each($events)) {
		$i++;
 		print "        <tr>
          <td bgcolor='#FF9900' width='10'>
         <input type='checkbox' name='".return_month_name($var)."[row".$i."][checkbox]' value='".$event_key."' onClick=\"checkBox('".return_month_name($var)."',".$i.")\" checked>
	 <input type='hidden' name='".return_month_name($var)."[row".$i."][present]' value='true'>
          </td>
          <td bgcolor='#FFFFCC' width='530' colspan='2'>
	 <input type='text' name='".return_month_name($var)."[row".$i."][day]' size='1' value='{$event_key}'>&nbsp;
	 <input type='text' name='".return_month_name($var)."[row".$i."][eventname]' size='50' value='".stripslashes($event_val)."'>
          </td>
        </tr>
";
    } //Secondary
    } //Master
 } // print function end
The summon for that is as follows: print_page(read_contents));

The code that writes to the file:

Code: Select all

function write_contents() {
    $JSarray = "";
    $JSarray = "events={}\n";
	for($j=0; $j < 12; ++$j)
	{
		$JSarray .= "events[{$j}]={\n";
		while(list($rowindex,$rowarray) = each($_POST[''.return_month_name($j).''])) {

	   		if ($rowindex != "row1") {
			$JSarray .= ",\n";
			} //endif

			if ($rowarray['present'] == "true") {
			   $JSarray .= "		".$rowarray['day'].":\"".addslashes($rowarray['eventname'])."\"";
			} //endif
		} //endwhile
        if ($_POST['action'] == "addfields" && $_POST['monthadd'] == return_month_name($j))
        {
        	for($i=($rowarray['day']+1); $i <= $_POST['nof']; $i++)
			{
			   $JSarray .= "		".$i.":\"Enter event here\"";
            } //endfor
        } //endif
		$JSarray .= "	}\n";
	} //endfor
    file_put_contents("events.script",$JSarray);
 } // write_contents end
Maybe you could tell what's wrong?

Posted: Fri Sep 30, 2005 1:16 am
by ruchit
just for try .. can you try removing addslashes (i know you're using stripslahes with the ouput function but just give it a try) from your write_contents function. I am sorry but couldn't go through the whole code... probably b'cos didn't sleep last night.

Posted: Fri Sep 30, 2005 3:12 am
by pilau
Problem solved - thanks to ruchit!

Posted: Fri Sep 30, 2005 3:16 am
by ruchit
glad that i could help :D

Posted: Fri Sep 30, 2005 3:26 am
by Jenk
I know this is "solved" but have you checked magic_quotes_runtime aswell as gpc? (get, post, cookies)