如何将PHP字符串数组放入HTML输入字段 [英] How to put a PHP array of strings into HTML input fields

查看:227
本文介绍了如何将PHP字符串数组放入HTML输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从php数组中获取元素,并将结果回显到文本输入字段列表中的网页。我的问题是$ list中的元素是字符串,由于某种原因,运行以下代码的结果只会将字符串的开头(直到第一个空格字符)放入输入字段中。

I am trying to take elements from a php array and echo the results to a webpage in a list of text input fields. My problem is that the elements within $list are strings and for some reason the result of running the following code only puts the beginning of the string (up to the first space character) into the input field.

例如,如果我的$ list元素之一是 hello world,则相应的输入字段仅显示 hello。我如何才能将整个字符串插入输入字段中?

For instance, if one of my $list elements is "hello world", the corresponding input field only says "hello". How can I get the entire string to be inserted into the input field?

function parse_array($list)
        {
            $new_string = "";
            foreach($list as $element)
            {
                $new_string .= "<li><input type='text' size=60 value=" . $element . "></li>";
            }
            return $new_string;
        }


推荐答案

代替此:

$new_string .= "<li><input type='text' size=60 value=" . $element . "></li>";

执行此操作:

$new_string .= '<li><input type="text" size=60 value="' . $element . '"></li>';

当需要在字符串中使用双qoutes时,应以单引号将其开始/结束容易得多。

When you need to use double qoutes in a string you should begin/end with single quotes that makes it much easier.

当您需要在字符串中使用单引号时,应以双引号开头/结尾,这样会使您更轻松。

When you need to use single quote in a string you should begin/end with double quotes, that makes it easier for you.

当您需要同时使用它们时,最好使用单引号。

When you need to use both you better use single quotes.

同样,双引号也会检查字符串中是否有变量。使用单引号时,必须使用'。 $ variable。 '

Also the thing with double quotes it will check if there is a variable inside the string. When you use single quote you must use ' . $variable . '

也可以查看手册以获取有关php中字符串的更多信息

Also take a look at the manual for more information about strings in php

http://www.php.net/manual/en/language .types.string.php

这篇关于如何将PHP字符串数组放入HTML输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆