当前位置 : 主页 > 网络编程 > PHP >

构建查询字符串

来源:互联网 收集:自由互联 发布时间:2021-06-28
构造一个链接,包含键值对 1. [代码] [PHP]代码 ?php /** * 构建查询字符串 */ $vars = array('name' = 'this is a name', 'color' = 'red', 'favorite_punctuation' = '#'); $query_str = http_build_query($vars); $url = '/test/te
构造一个链接,包含键值对

1. [代码][PHP]代码    

  <?php
  /**
   * 构建查询字符串
   */
   
  $vars = array('name' => 'this is a name',
                'color' => 'red',
                'favorite_punctuation' => '#');
  $query_str = http_build_query($vars);
  
  $url = '/test/test.php?' . $query_str;
  echo $url."\n";
  
  $url = '/test/test.php?' . htmlentities($query_str);
  echo $url."\n";

输出结果:
/test/test.php?name=this+is+a+name&color=red&favorite_punctuation=%23
/test/test.php?name=this+is+a+name&amp;color=red&amp;favorite_punctuation=%23


~                                                                                                                                                                                                           
~                    
网友评论