如何使用 fwrite 将引号写入 .txt 文件
我有一个 html 表单,用户可以在其中提交数据。当他们提交时,我将获取该数据并将其写入 .txt 文件。我在数据周围添加引号时遇到问题。 我需要它在写入 txt 文件时执行以下操作。
$name = $_POST['用户'];
$data= "$name"
var Name="$data",
所以如果他用户在表单上输入 John,我需要它来读取: var Name="John",
在我的数据表单上。任何帮助表示赞赏。我尝试过addslashes,但似乎也无法使其正常工作。
I have an html form where users can submit data. When they submit I am taking that data and writing it to a .txt file. I am having trouble adding quotation marks around my data.
I need it to do the following when it writes to the txt file.
$name = $_POST['user'];
$data= "$name"
var Name="$data",
So if he user entered John on the form, I would need it to read:
var Name="John",
On my data form. Any help is appreciated. I have tried addslashes, but can't seem to get that to work correctly either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
try this:
编程中有一些看似奇怪的怪癖,但特别是阻止您这样做的一个事实是您没有将引号 (") 视为字符串。Php 对此进行解释:
作为将 $name 转换为 a 的请求string 并将其存储在 $data 中,您想要的是将引号用单引号括起来,以便 php 将它们转换为字符串,然后将它们与您的连接(使用连接运算符,句点 (.) )。 $name 变量,如下所示:
$data 现在保存值:“John” 最后,对于您要写入的实际字符串,您可以将新变量与“var Name=”部分连接起来:
因此 $string 的值 。现在保存字符串:
var Name="John"
这正是您想要的。
There are quirks in programming that seem strange, but in particular the one preventing you from doing this is the fact that you are not treating the quotation marks (") as strings. Php is interpreting this:
As a request to turn $name into a string and store it inside of $data. What you want is to wrap the quotation marks with single quotations, so that php will turn them into a string. Then you concatenate (with the concatenation operator, the period (.) ) those with your $name variable, like so:
$data now holds the value: "John". Finally, for the actual string you want to write, you would concatenate your new variable with the 'var Name=' part:
So the value of $string now holds the string:
var Name="John"
Which is exactly what you want.