【发布时间】:2012-10-10 16:11:00
【问题描述】:
可能重复:
How can I split a comma delimited string into an array in PHP?
以domain.com/?q[]=a_1&q[]=a_2&q[]=a_3的示例URL格式:
在选择选项中,我将使用以下代码检索selected='selected':
<option value="a_1"<?php if(isset($_GET['q'])) if (in_array('a_1', $_GET['q'])) { echo ' selected="selected"'; } ?>>
Sample 1
</option>
<option value="a_2"<?php if(isset($_GET['q'])) if (in_array('a_2', $_GET['q'])) { echo ' selected="selected"'; } ?>>
Sample 2
</option>
<option value="a_3"<?php if(isset($_GET['q'])) if (in_array('a_3', $_GET['q'])) { echo ' selected="selected"'; } ?>>
Sample 3
</option>
如果 URL 格式改为 domain.com/?q=a_1,a_2,a_3,我将如何检索所选值?
【问题讨论】:
-
要么先将其设为数组 (str_getcsv),要么改为搜索字符串部分 (strstr)。