i'm creating a html editor using JavaScript and php, where the user selects html element from browser, edits this element inline and submits the changes to the server.
Now to apply the changes, i've used str_replace
to compare the changes sent by the user with existing content in the html file at server end and replace it.
The selected element's content($_POST['content1']
) and new content($_POST['contentreplace1']
) are sent using $_POST
But this functionality is not working properly.
The existing code is not getting replaced with the new one.
<?php
$orig="".trim($_POST['content1']);
$new="".trim($_POST['contentreplace1']);
$file = 'index.html';
$file_contents = file_get_contents($file);
$file_contents_new = str_replace($orig,$new,$file_contents);
file_put_contents($file,$file_contents_new);
?>
What I'm doing wrong here?
Any suggestions or recommendations are welcomed. Please share your thoughts and experiences. Thanks for all the help in advance.