简体   繁体   中英

How to interpret HTML as plain Text inside textarea?

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. :)

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>

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.)

ie 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 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 &lt/textarea&gt .

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 &lt &gt for the end of textarea block
  $Body=preg_replace("%</textarea>%/i","&lt/textarea&gt", $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 file (like HTML): 文件(如HTML):

You have to search for the </textarea> and can just use ".$Body."

ie

$filetype=pathinfo($File);
$filetype['extension'];
$php_files=Array('php','php3','php5');
if (!in_array($filetype['extension'], $php_files)) {  

$Body=preg_replace("%</textarea>%/i","&lt/textarea&gt", $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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM