troubleshooting Question

How to save DOM content without encoding issues?

Avatar of JohnLourdu
JohnLourduFlag for Afghanistan asked on
PHPJavaScriptResponsive Web
1 Comment1 Solution569 ViewsLast Modified:
I have creating ePub editor in the browser.
After uploading ePub files to server, I have loading the each html file using "iframe".
Loaded iframe content is located in same domain.
I have dynamically loaded iframe content like this:
<iframe scrolling="no" frameborder="0" height="100%" class="readium-flowing-content" style="position: relative; z-index: 0; top: 0px; left: 0px; width: 956px; height: 436px; padding-left: 53px; padding-right: 53px; margin-left: 0px;" src="epub_content/siddhartha3/OPS/s007-Part-001.xhtml" title="Siddhartha - EPUB3"></iframe>
Using HTML5 contenteditable attribute I have saved edited iframe content using ajax and php.
My php code is:
<?php
session_start();
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

$content = $_POST['curhtmlbody']; 
$element_id = $_SESSION['curhtmlurl'];

//$content = 'John Lourdu samy'; 
//$element_id = 'epub_content/siddhartha3/OPS/s007-Part-001.xhtml';



if (get_magic_quotes_gpc()) {
function stripslashes_gpc(&$value) {
$value = stripslashes($value);
}
array_walk_recursive($_GET, 'stripslashes_gpc');
array_walk_recursive($_POST, 'stripslashes_gpc');
array_walk_recursive($_COOKIE, 'stripslashes_gpc');
array_walk_recursive($_REQUEST, 'stripslashes_gpc');
}

$newcont='<body>'.stripslashes($content).'</body>';

//get the file contents
$file = file_get_contents($element_id);
//use regex to replace what's in between the body tags
$file = preg_replace("/\<body\>(.*)\<\/body\>/Uis",$newcont, $file); 

//open the file in write mode
$myFile = $element_id;
$fh = fopen($myFile, 'w') or die("can't open file");
//write the new contents to the file
fwrite($fh, $file);
fclose($fh); 

header("location:page1.php");
exit();
?>

My problem is: saved content has encoding issues. It removes all decimal/hex entities to literal characters. How to solve this issue. Please advice.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros