Reputation:
I am trying to make a online code editor, so that I don't have to upload modified files to the website each time. To edit the contents of a file, the code gets its contents and puts them into a textarea in form of text like this:
<textarea id="text" name="contents">
<?php
include_once "useful.php";
if(isset($_GET["url"])){
echo file_get_contents($_GET["url"]);
}
?>
</textarea>
The Problem is the following:
If the file has a </textarea>
tag inside, this closes my textarea tag closes without displaying the entire file.
Is there a simple solution to solve this? Thank you in advance. :)
Upvotes: 1
Views: 1779
Reputation: 11
html_entity_decode()
and some bracket conversion enabled me to display the HTML code <textarea></textarea>
within a <textarea>
block.
This is my first reply here, so please be kind. :)
I had to fix a PHP text file editor which would output its file contents within a <textarea></textarea>
window -- however, if the file it reads contained <textarea></textarea>
blocks, the output window would close the block and interpret it as HTML and the closing </textarea>
and the rest of the file would not be displayed in the editor window.
(In other words, it is a PHP script which loads files and displays their contents inside of a <textarea></textarea>
window for editing, etc.)
i.e. a file contained:
Enter stuff here: <textarea name="blah"></textarea> Yay Stuff.
it displayed as:
Enter stuff here: <textarea name="blah">
(missing all other content!)
There were two types of interpretation needed.
For a PHP file:
After beating myself up and with some help from this site, I was able to trick the interpreter by 'sanitizing' the block by first converting the ending </textarea>
to </textarea>
.
ex: ($Body is just the contents of the file to display in the editor)
//if reading a <textarea block of code
if (preg_match("%<textarea%/i",$Body)) {
//turn < > into < > for the end of textarea block
$Body=preg_replace("%</textarea>%/i","</textarea>", $Body);
//sanitize (this is the output window where the file content goes)
print html_entity_decode("</font><textarea>" .$Body. "</textarea>\n");
}
This allowed the window to correctly output the file content such as:
Enter stuff here: <textarea name="blah"></textarea>
Yay stuff.
For a non-PHP file (like HTML):
You have to search for the </textarea>
and can just use ".$Body."
i.e.
$filetype=pathinfo($File);
$filetype['extension'];
$php_files=Array('php','php3','php5');
if (!in_array($filetype['extension'], $php_files)) {
$Body=preg_replace("%</textarea>%/i","</textarea>", $Body);
echo "</font><textarea> ".$Body." </textarea>\n";
}
Note: If a PHP file did -not- contain a <textarea>
it would be fine to use:
print html_entity_decode($Body)
and I did have to contend for that as well.
Note2: I am displaying PHP 5+ code, but I had to do this for PHP 5, and just FYI it works fine with eregi() and eregi_replace()
It drove me mad, so I hope this helps someone else. ...mad enough that I came back again and updated my answer. :)
Upvotes: 0
Reputation: 7441
htmlspecialchars
function will solve your problem.
This code does what you want:
<textarea>
<?php
//Fake file content
$fileContent = "</textarea> with something";
print htmlspecialchars($fileContent);
?>
</textarea>
Upvotes: 1