我是使用TinyMCE的新手,但对尖括号的行为感到沮丧。它似乎是将<foo>
或<foo>Foo</foo>
等输入解释为标记,尽管页面来源显示两种情况分别转换为<foo>
和<foo>Foo</foo>
我减少了我的代码,它在下面:
<?php
// Simplified for SO, no file writing / reading
$content = isset($_POST["forSo"]) ? $_POST["forSo"] : "";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>For Stack Overflow</title>
<script src="/tinymce/js/tinymce/tinymce.min.js"></script>
<script>
tinymce.init({ selector : "#forSo" });
</script>
</head>
<body>
<?php
// Behaves as expected, TinyMCE correctly automatically converts HTML Entities
echo $content . "\n";
?>
<form action="/forSo.php" method="post" enctype="multipart/form-data">
<textarea id="forSo" name="forSo">
<?php
// Page source shows that this has HTML Entities, still loses information
echo $content . "\n";
?>
</textarea><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
如果我输入说<foo>
,则结果页面来源为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>For Stack Overflow</title>
<script src="/tinymce/js/tinymce/tinymce.min.js"></script>
<script>
tinymce.init({ selector : "#forSo" });
</script>
</head>
<body>
<p> <foo></p>
<form action="/forSo.php" method="post" enctype="multipart/form-data">
<textarea id="forSo" name="forSo">
<p> <foo></p>
</textarea><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
然而,TinyMCE似乎抛弃了textarea的内容导致了这一点(再次点击提交会导致所有$content
为空字符串):
答案 0 :(得分:0)
用<?php echo $content . "\n"; ?>
替换textarea内的第二个<?php echo str_replace("&", "&", $content) . "\n"; ?>
来完成任务
这确实会阻止编写有效的HTML标记,但是文本意味着以<foo>
为字面意思的文本将作为&lt;foo&gt;
保留在TinyMCE编辑器中,而不是<foo>
,而TinyMCE将其解释为HTML标记