[Solved] Backslashing problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

[Solved] Backslashing problem

Post 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?
Last edited by pilau on Fri Sep 30, 2005 3:12 am, edited 1 time in total.
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post 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
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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?
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post 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.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Problem solved - thanks to ruchit!
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post by ruchit »

glad that i could help :D
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I know this is "solved" but have you checked magic_quotes_runtime aswell as gpc? (get, post, cookies)
Post Reply