I have two array Session variables for my test. One is a group of answers the other is a group of questions. Upon submitting the form and going to the page where the test is graded one of my session arrays maintains its form while the other adds a backslashes () and messes up the code.
So one Session array looks like \[y'=x\]
(which is the way it should look) and the other looks like \\[y\'=x\]
(with added backslashes)
The only really difference is where the information comes from, the one that works comes from a database directly, the one that does not comes from a form input that gets its A,B,C,D options from the database. So all options come from the same database there is just a middle man in the one that does not work.
Try with this, it may be solve your problem. I am not 100% sure
$name = "O'Reilly & Associates";
$name = wp_slash($name);
echo $name;
Generally in PHP whenever you want to add ' in the string, You need to add string operators like backslash \ and wordpress handles it by default. So you are getting that \ added automatically. Try with wp_slash(), It may helps you to solve problem.