PHP $_REQUEST 作为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1543950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:01:13  来源:igfitidea点击:

PHP $_REQUEST as array

phparraysrequest

提问by chaos

I have a search form, I want to $_REQUEST the search terms as an array so I can list each search term out, wrapping each term in a span for styling. How do I do that?

我有一个搜索表单,我想将搜索词 $_REQUEST 作为一个数组,这样我就可以列出每个搜索词,将每个词包装在一个跨度中以进行样式设置。我怎么做?

Edit: Here's the code requested.

编辑:这是请求的代码。

<form action="http://localhost/wordpress" id="search" method="get">
<input type="text" size="30" id="s" name="s" value="Type and hit enter" onfocus="javascript:this.value='';" onblur="javascript:this.value='Type and hit enter';"/>
<br/>
<input type="submit" value="Search"/>
</form>

Update: Thanks guys for the responses. I'll use explode, it seems fairly straightforward. Plus the name sounds cool ^^

更新:谢谢大家的回答。我将使用爆炸,它看起来相当简单。加上名字听起来很酷^^

回答by chaos

In the form:

在形式:

<input type="text" name="terms[]" />
<input type="text" name="terms[]" />
<input type="text" name="terms[]" />

In the form processor:

在表单处理器中:

<? foreach($_REQUEST['terms'] as $term) { ?>
    <span style="searchterm"><?= htmlspecialchars($term) ?></span>
<? } ?>

回答by Ben Gribaudo

If you want the user to enter multiple search terms in separate input controls, the above answers should be helpful. However, your example form leads me to wonder if you want to use only one search phrase input text box. If that's so, this might be what you're looking for:

如果您希望用户在单独的输入控件中输入多个搜索词,上述答案应该会有所帮助。但是,您的示例表单让我想知道您是否只想使用一个搜索词组输入文本框。如果是这样,这可能是您正在寻找的:

<?php
  $searchTerms = preg_split("/[\s,]+/", $_REQUEST['SearchTerms']);

   foreach($searchTerms as $term) { ?>
     <span class="term"><?= htmlentities($term) ?></span>
<? 
   }
?>

回答by Bj?rn H. Sandvik

I did it this way..

我是这样做的。。

Passing an array (it's really just a string to PHP):

传递一个数组(它实际上只是一个字符串到 PHP):

http://www.somesite.net/myscript.php?myArray=Planes,Trains,Automobiles

http://www.somesite.net/myscript.php?myArray=飞机、火车、汽车

Then in the script, just explode the string:

然后在脚本中,只需分解字符串:

$myArray = explode(",", $_REQUEST['myArray']);

$myArray = expand(",", $_REQUEST['myArray']);

Maybe not exactly what you're looking for.

也许不完全是你要找的。

回答by antyrat

If you want break your search terms by space symbols just try this code:

如果你想用空格符号打破你的搜索词,试试这个代码:

<?php
   $search_terms = explode(" ", $_REQUEST['s']);
   foreach($search_terms AS $search_term_item) {
     echo "<span class=\"SearchTerm\">".htmlspecialchars($search_term_item)."</span>";
   }
?>

回答by stroop

I figure you wish the user to have one single entry input, which you then wish to split into an array of separate search terms.

我认为您希望用户有一个单一的条目输入,然后您希望将其拆分为一组单独的搜索词。

Split your input on whitespace (treating consecutive whitespace characters as one) to derive separate terms.

将您的输入拆分为空格(将连续的空格字符视为一个)以导出单独的术语。

For example :

例如 :

$termList = preg_split("/\s+/", trim($_REQUEST['s']));
foreach($termList as $term) { echo "<span>".htmlspecialchars($term)."</span>\n"; }

Ofcourse do not forget to properly filter and escape the input before you use it.

当然,在使用之前不要忘记正确过滤和转义输入。

回答by Gary Willoughby

In your HTML form element you can assign the name to an array, like this:

在您的 HTML 表单元素中,您可以将名称分配给一个数组,如下所示:

<select id="MySelect" multiple="multiple" name="SearchTerms[]" class="MyClass">
    ...
</select>

then when you deal with the form after submittion you can do something like:

然后当您在提交后处理表单时,您可以执行以下操作:

<?php
    foreach($_REQUEST['SearchTerms'] as $SearchTerm)
    {
        Print("<span class=\"SearchTerm\">$SearchTerm</span>");
    }
?>

回答by Ben Gribaudo

Here's more details on passing in form results as an array: http://us.php.net/manual/en/faq.html.php#faq.html.arrays

以下是有关将表单结果作为数组传递的更多详细信息:http: //us.php.net/manual/en/faq.html.php#faq.html.arrays