PHP 输入带有变量的文本

发布于 2025-01-05 22:56:08 字数 346 浏览 3 评论 0原文

考虑以下用于从文本文件打印问题的代码:

    foreach ($lines as $line_num => $line) {
        if($line_num%3 == 1){
            echo 'Question '.$count.':'.'<br/>'.'<input type="text" value="$line" class="tcs"/>'.'<br/>';

我已经尝试了许多字符串转义组合。问题是我得到的是 $line 在文本字段内而不是变量值。非常感谢任何帮助。

Consider the following code for printing questions from text file:

    foreach ($lines as $line_num => $line) {
        if($line_num%3 == 1){
            echo 'Question '.$count.':'.'<br/>'.'<input type="text" value="$line" class="tcs"/>'.'<br/>';

I've tried many string escaping combinations. The problem is that I get $line inside the text field instead of the variable value. Any help is greatly appreciated.

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

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

发布评论

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

评论(3

近箐 2025-01-12 22:56:08

' 带引号的字符串中删除变量,或使用 " 以便解释变量。

echo 'Question ' . $count . ':<br/><input type="text" value="' . $line . '" class="tcs"/><br/>';

或者

echo "Question " . $count . ":<br/><input type=\"text\" value=\"$line\" class=\"tcs\"/><br/>";

第一个选项更好,因为您不必转义其他任何内容。

Remove the variable from the ' quoted string, or use " so the variable is interpreted.

echo 'Question ' . $count . ':<br/><input type="text" value="' . $line . '" class="tcs"/><br/>';

or

echo "Question " . $count . ":<br/><input type=\"text\" value=\"$line\" class=\"tcs\"/><br/>";

The first option is better, since you don't have to escape anything else.

心安伴我暖 2025-01-12 22:56:08

你尝试过吗:

echo 'Question ' . $count . ':'.'<br/>'.'<input type="text" value="' . $line . '" class="tcs"/>'.'<br/>';

Did you try:

echo 'Question ' . $count . ':'.'<br/>'.'<input type="text" value="' . $line . '" class="tcs"/>'.'<br/>';
自此以后,行同陌路 2025-01-12 22:56:08

变量不会在单引号字符串中进行处理。您需要使用双引号或其他插入方式(例如串联)。

Variables don't get processed in single quoted strings. You need to use double quotes or another way of inserting them (such as concatenation).

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