Textarea file edit php

2.3k Views Asked by At

I have a PHP script for editing files, html, php, etc..

It is working in it's entirety except, when you enter into the textarea form field, for example:

<div>
& nbsp; or & amp;
</div>

the special characters are automatically converted to just a space, and just an ampersand &.

But the purpose of a file editor is to enter exactly what you need, and have it displayed, as html code may or may not have to been written in entity form, and I do not want them automatically converted to their non-entity form. htmlspecialchars or htmlentities does not work because then the entire document is converted to special characters

a view source of the document when using htmlspecialchars is:

&lt;!DOCTYPE html PUBLIC&gt;

&lt;html&gt;

&lt;head&gt;

&lt;title&gt;Test&lt;/title&gt;

etc...

and then it is outputed in the browser as:

<!DOCTYPE html PUBLIC> <html> <head> <title>Test</title> </head> <body> TEST  x &nbsp; </body> </html>

I have this one and only dilemma. If a just simply remove htmlspecialchars from processing the post data, then, it all works perfectly fine, except the html entities are automatically converted to the readable form, & nbsp; to space, and & amp; to ampersand &

Any ideas? I know cPanel managed to do this somehow in their file editor, and in net2ftp, and many other web based file text-editors.

Thanks

1

There are 1 best solutions below

0
On

If you are using htmlspecialchars, it is going to turn anything that is not alphanumeric into an html entity, so it will actually render the symbol in the browser rather than using it as markup. You could probably store whatever the user enters as plain text, then just echo it back on to the page when it needs to be displayed. This code worked when I tried it for I believe what you are trying to do.

<?php
 $var = $_POST['var'];
 echo $var;

 ?>

<form id='form' action='' method='post'/>
<textarea form='form' name='var'></textarea>
<input type='submit' value='submit'/>