php html 代码作为 textarea 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18315588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:20:11  来源:igfitidea点击:

html code as a value for textarea

phphtmltextareacdata

提问by pastx

I am working on simple code editor and I have this simple HTML page:

我正在研究简单的代码编辑器,我有这个简单的 HTML 页面:

<div class="container">
<h1>Test</h1>
<p>Hello world !</p>
</div>

I need to create simple form that would allow me to edit this page and save changes, this is my code:

我需要创建允许我编辑此页面并保存更改的简单表单,这是我的代码:

<?php
  $text = file_get_contents("test.html");
?>
<form method="POST">
    <fieldset>
        <textarea name="text"  value="<?php echo $text;  ?>" >
        </textarea>
        <button type="submit" class="btn">Send</button>
    </fieldset>
</form>

Unfortunately this doesnt work right and it does not parse all html code as text , this is real value of textarea after runing script

不幸的是,这行不通,它不会将所有 html 代码解析为文本,这是运行脚本后 textarea 的真正价值

<h1>Test</h1>
    <p>Hello world !</p>
</div>" >

It looks like it parsed div with class declaration as valid html code, not as text.

看起来它将带有类声明的 div 解析为有效的 html 代码,而不是文本。

Probably caused by ".

大概是 引起的"

I also try this but result was exactly same:

我也试过这个,但结果完全一样:

<textarea name="text"  value="<?php echo "<![CDATA[".$text."]]>";  ?>" >

Can anyone help me to get all content of html page as value of textarea ?

任何人都可以帮助我获取 html 页面的所有内容作为 textarea 的值吗?

回答by piddl0r

To add data to the text area you need to echo between <textarea>tags, not as a value. (textareadocumentation).

要将数据添加到文本区域,您需要在<textarea>标签之间回显,而不是作为值。(文本区域文档)。

<textarea name="text"><?php echo $text;?></textarea>

回答by Lkopo

You need to include your values between tags <textarea></textarea>:

您需要在标签之间包含您的值<textarea></textarea>

<textarea name="text"><?php echo $text; ?></textarea>

回答by VIVEK-MDU

try this

尝试这个

It simple this

这很简单

<textarea name="text" rows="10" cols="50"><?php echo $text; ?></textarea>

Here colsand rowsare the attribute of textarea.you can't place you value in that place.you can place your value <?PHP echo $text;?>in between <textarea></textarea>

这里colsrows是的属性textarea。你不能把你的值在place.you可以把你的价值<?PHP echo $text;?>之间<textarea></textarea>

回答by Ananth

You can use the one of the following

您可以使用以下之一

<textarea name="text"><?php echo $text;  ?></textarea>

or 

<textarea name="text"><?php echo htmlentities($text)  ?></textarea>

or 

<textarea name="text"><?php echo htmlspecialchars($text)  ?></textarea>