Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

XML Special Chars Issue

Posted on 2008-06-15
6
245 Views
Last Modified: 2008-06-15
Hi everyone,
I made a flash-based interface website that enables me to update data in an XML-file using a PHP script.
Everything works fine, but I have some trouble with Special Characters like : '
When I enter something like "Band's" , it gets written away like this : "Band\'s" . Everytime I resave the data an extra backslash gets added, which reslults in : "Band\\\\\\'s".
Obviously this isn't the desired result. Is there anyway to avoid this?
Should I adapt my Flash script, or can this be solved by altering the PHP script?
I'm not supllying any code for now, because I think there must be some standard answers to this.
If you do want me to post the script, please let me know !

Kind Regards,
Dreammonkey
0
Comment
Question by:Dreammonkey
  • 2
  • 2
  • 2
6 Comments
 
LVL 29

Expert Comment

by:fibo
ID: 21789237
Get magic quotes strike again... before writing to sql (and also after reading) a string variable or a string array $my_text, I would call fn_Corrige_Slashes($my_text) which would put all my strings in a coherent and stable state.

I use the following functions:
function fn_Corrige_slashes ($debut) {
      $tmp = is_scalar($debut)?
            fn_scalaire_Corrige_slashes ($debut)
            :array_map_recursive('fn_scalaire_Corrige_slashes',$debut);
      return $tmp;
}
      function fn_scalaire_Corrige_slashes ($debut) {
      $tmp=trim($debut);
//      echo "dans corrige [$tmp] --- ";
      if (get_magic_quotes_gpc()) {
            $tmp= stripslashes($tmp);
      };
//      echo "corrslashes ? av|$debut| après|$tmp|<br>";
      return $tmp;
}
0
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 21789243
Most likely the so called magic quotes http://us3.php.net/magic_quotes are enabled on the server resulting in a \ being automatically added to all POST and GET variables. You can use the stripslashes function http://us3.php.net/manual/en/function.stripslashes.php to remove them, e.g. when you are saving the variable:

// Prior to saving the data check if magic quotes are enabled, if yes remove the
//  automatically added \
if (get_magic_quotes_gpc() == 1) {
    $sYourVariable = stripslashes($sYourVariable);
}
0
 
LVL 29

Expert Comment

by:fibo
ID: 21789267
B-)) I had written that a long time ago...
When reading the string or getting it from get/post/cookie or mysql : use fn_Corrige_Slashes which will remove slashes if needed (this depends on the setting of get_magic_quotes_gpc)
When saving the string in Mysql: it is usually escaped as needed, so no action is required

One of the problems is that get_magic_quotes_gpc setting changes form system to system or even version. Using a "defensive programming" like this function will ensure that not only it does work on your current config, bt also that it should work in other configurations.

Not sure how this is affected by Flash though... some experimentation might be needed!
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 8

Author Comment

by:Dreammonkey
ID: 21789638
Right guys,
thanks a lot, You're solutions all seem to be PHP-minded, which is OK. I would have liked a AS-based answer but hey, you can't win 'm all...

So LordofPorts, you litle function there seems highly transparant. Should I emplement it on all my Variables like in the code below?
<?php
$id = $_POST['id'];
$band = $_POST['band'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$venue = $_POST['venue'];
$time = $_POST['time'];
$info = $_POST['info'];
$address = $_POST['address'];
$location = $_POST['location'];
$moreInfo = $_POST['moreInfo'];
$link1 = $_POST['link1'];
$link2 = $_POST['link2'];
if (get_magic_quotes_gpc() == 1) {
    $sid = stripslashes($sid);
    $sband = stripslashes($sband);
    $sday = stripslashes($sday);
    // and so on .... ?
}
 
 
if (file_exists('gigs.xml'))
  {
	$xml = simplexml_load_file('gigs.xml');
	
	$newgig = $xml->addchild('gig');
	
	$newgig->addAttribute('id', $id);
	$newgig->addAttribute('band', $band);
	$newgig->addAttribute('day', $day);
	$newgig->addAttribute('month', $month);
	$newgig->addAttribute('year', $year);
	$newgig->addAttribute('venue', $venue);
	$newgig->addAttribute('time', $time);
	$newgig->addAttribute('info', $info);
	$newgig->addAttribute('address', $address);
	$newgig->addAttribute('location', $location);
	$newgig->addAttribute('moreInfo', $moreInfo);
	$newgig->addAttribute('link1', $link1);
	$newgig->addAttribute('link2', $link2);	
  }
echo $xml->asXML('gigs.xml');

Open in new window

0
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 500 total points
ID: 21789841
If none of the elements in the $_POST array are an array themselves as seems to be the case in your code above the easiest would be to place the following line at the top:

if (get_magic_quotes_gpc() == 1) {
      $_POST = array_map("stripslashes", $_POST);
}
<?php
 
if (get_magic_quotes_gpc() == 1) {
	$_POST = array_map("stripslashes", $_POST);
}
 
$id = $_POST['id'];
$band = $_POST['band'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$venue = $_POST['venue'];
$time = $_POST['time'];
$info = $_POST['info'];
$address = $_POST['address'];
$location = $_POST['location'];
$moreInfo = $_POST['moreInfo'];
$link1 = $_POST['link1'];
$link2 = $_POST['link2'];
 
if (file_exists('gigs.xml'))
  {
	$xml = simplexml_load_file('gigs.xml');
	
	$newgig = $xml->addchild('gig');
	
	$newgig->addAttribute('id', $id);
	$newgig->addAttribute('band', $band);
	$newgig->addAttribute('day', $day);
	$newgig->addAttribute('month', $month);
	$newgig->addAttribute('year', $year);
	$newgig->addAttribute('venue', $venue);
	$newgig->addAttribute('time', $time);
	$newgig->addAttribute('info', $info);
	$newgig->addAttribute('address', $address);
	$newgig->addAttribute('location', $location);
	$newgig->addAttribute('moreInfo', $moreInfo);
	$newgig->addAttribute('link1', $link1);
	$newgig->addAttribute('link2', $link2);	
  }
 
echo $xml->asXML('gigs.xml'); 
 
?>

Open in new window

0
 
LVL 8

Author Comment

by:Dreammonkey
ID: 21789860
LordOfPorts, you rock !
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question