PHP $_REQUEST 作为数组

发布于 2024-08-07 01:10:40 字数 488 浏览 4 评论 0原文

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

编辑:这是请求的代码。

<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>

更新:谢谢大家的回复。我将使用爆炸,它看起来相当简单。而且这个名字听起来很酷^^

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?

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 ^^

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

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

发布评论

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

评论(7

少年亿悲伤 2024-08-14 01:10:40

在表单中:

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

在表单处理器中:

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

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>
<? } ?>
街角卖回忆 2024-08-14 01:10:40

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

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

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

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>
<? 
   }
?>
娇女薄笑 2024-08-14 01:10:40

我这样做了......

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

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

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

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

也许不正是您正在寻找的东西。

I did it this way..

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

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

Then in the script, just explode the string:

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

Maybe not exactly what you're looking for.

昔梦 2024-08-14 01:10:40

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

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

例如:

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

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

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.

云柯 2024-08-14 01:10:40

如果您想用空格符号分隔搜索词,只需尝试以下代码:

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

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>";
   }
?>
桃扇骨 2024-08-14 01:10:40

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

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

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

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

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

<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>");
    }
?>
落花随流水 2024-08-14 01:10:40

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

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

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