如何从PHP中的URL获取具有相同名称的多个参数
我有一个PHP应用程序,有时需要处理URL,其中URL中的多个参数具有相同的名称。是否有一种简单的方法来检索给定键的所有值?PHP $
_GET仅返回最后一个值。
具体来说,我的应用程序是一个OpenURL解析器,可能会获得如下URL参数:
ctx_ver=Z39.88-2004&rft_id=info:oclcnum/1903126
&rft_id=http://www.biodiversitylibrary.org/bibliography/4323
&rft_val_fmt=info:ofi/fmt:kev:mtx:book
&rft.genre=book
&rft.btitle=At last: a Christmas in the West Indies. 
&rft.place=London,
&rft.pub=Macmillan and co.,
&rft.aufirst=Charles
&rft.aulast=Kingsley
&rft.au=Kingsley, Charles,
&rft.pages=1-352
&rft.tpages=352
&rft.date=1871
(是的,我知道这很丑陋,欢迎来到我的世界)。请注意,键“ rft_id”出现两次:
rft_id=info:oclcnum/1903126rft_id=http://www.biodiversitylibrary.org/bibliography/4323
$_GET将返回just
http://www.biodiversitylibrary.org/bibliography/4323,先前的值(info:oclcnum/1903126)已被覆盖。
我想同时访问这两个值。这在PHP中可行吗?如果没有,对如何处理此问题有什么想法?
回答:
就像是:
$query  = explode('&', $_SERVER['QUERY_STRING']);$params = array();
foreach( $query as $param )
{
  // prevent notice on explode() if $param has no '='
  if (strpos($param, '=') === false) $param += '=';
  list($name, $value) = explode('=', $param, 2);
  $params[urldecode($name)][] = urldecode($value);
}
给你:
array(  'ctx_ver'     => array('Z39.88-2004'),
  'rft_id'      => array('info:oclcnum/1903126', 'http://www.biodiversitylibrary.org/bibliography/4323'),
  'rft_val_fmt' => array('info:ofi/fmt:kev:mtx:book'),
  'rft.genre'   => array('book'),
  'rft.btitle'  => array('At last: a Christmas in the West Indies.'),
  'rft.place'   => array('London'),
  'rft.pub'     => array('Macmillan and co.'),
  'rft.aufirst' => array('Charles'),
  'rft.aulast'  => array('Kingsley'),
  'rft.au'      => array('Kingsley, Charles'),
  'rft.pages'   => array('1-352'),
  'rft.tpages'  => array('352'),
  'rft.date'    => array('1871')
)
由于总是有可能重复一个URL参数,因此最好总是具有数组,而不是仅对那些预期它们的参数进行重复。
以上是 如何从PHP中的URL获取具有相同名称的多个参数 的全部内容, 来源链接: utcz.com/qa/415688.html


