存储 PHP 变量时出现问题

发布于 2025-01-04 07:46:04 字数 1088 浏览 2 评论 0原文

首先,我认为自己在 PHP 方面处于初级和中级之间。在我的网站上,我有一个文本区域,用户可以在其中输入 Web 代码,按下按钮时,会将代码发送到页面并显示输出。以下是该页面的链接:http://opensourcewebsite.host22.com/editpage.php 。我遇到的另一个问题是我希望用户输入的任何内容都保留在页面上。

到目前为止,代码如下所示:

<?php $source_code = $_POST['source-code'}; ?>
...
<editable_area>
<?php echo $source_code ?>
</editable_area>
...
<form action="" method="post">
input class="result" type="submit" name="submit" value="View Result">
<textarea id="source" class="edit_are" name="source-code"></textarea>

目前,当用户输入包含 id/class/name/etc 的代码时。其中有引号,就像您这样做时一样:

echo "<div id="Name">"

它将添加 \" ,从而弄乱代码。我需要找到一种方法来存储代码,以便它能够正确显示。如果您尝试该网页, 当提交代码时,你会看到我的问题


,textarea通过javascript获取网页的源代码,我发现如果你在代码中省略“”,它会按预期工作。当它获取源代码时,有引号这样代码就会在文本中显示它们,这意味着每次进行更改时,您都需要取出所有引号,


感谢 webbiedave,我通过使用以下方法解决了引号问题:

<?php echo stripslashes($source_code) ?>

现在我只需要弄清楚。如何永久存储更改。

to start I consider myself in between beginner and intermediate when it comes to PHP. On my website, I have a textarea that the user can input web code in and when a button is pressed, it sends the code to the page and displays the output. Here is the link to the page: http://opensourcewebsite.host22.com/editpage.php. Another problem that I am running into is that I want whatever the user enters to stay on the page.

Here is what the code looks like so far:

<?php $source_code = $_POST['source-code'}; ?>
...
<editable_area>
<?php echo $source_code ?>
</editable_area>
...
<form action="" method="post">
input class="result" type="submit" name="submit" value="View Result">
<textarea id="source" class="edit_are" name="source-code"></textarea>

Currently, when a user inputs code containing an id/class/name/etc. that has quotes in it, it is like when you do:

echo "<div id="Name">"

It will add the \" and thus messing up the code. I need to find a way to store the code so that it will display correctly. If you try the web page, you will see my issue first hand.


When the code is submitted, the textarea grabs the source code of the web page through javascript. I have discovered that if you leave out the "" in the code, it works as expected. The problem is that when it grabs the source code, there is quotes in it so the code will display them in the textare. This means every time you make a change, you need to take out all of the quotes.


Thanks to webbiedave, I fixed the quote problem by using:

<?php echo stripslashes($source_code) ?>

Now I just need to figure out how to permanently store changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月打碎记忆 2025-01-11 07:46:04
<?php echo htmlentities(stripslashes($source_code)) ?>
<?php echo htmlentities(stripslashes($source_code)) ?>
放低过去 2025-01-11 07:46:04

检查您是否开启了魔法引号并确保关闭它们。另外,您需要使用 htmlspecialchars 来转义特殊字符。否则,他们可能会关闭您的 textarea 并造成其他严重破坏。

echo htmlspecialchars($source_code);

Check if you have magic quotes turned on and make sure you turn them off. Also, you'll want to use htmlspecialchars to escape the special characters. Otherwise, they can just close your textarea and wreak other havoc.

echo htmlspecialchars($source_code);
愚人国度 2025-01-11 07:46:04

您可以改用单引号吗:
'

';

Can you use single quotes instead:
'<div id="Name">';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文