Home  >  Article  >  Backend Development  >  How to convert PHP array to URI string

How to convert PHP array to URI string

PHPz
PHPzOriginal
2023-04-20 15:02:38549browse

In web development, we often need to convert PHP arrays into URIs. URI (Uniform Resource Identifier) ​​or URL (Uniform Resource Locator) is a string identifier used to indicate a specific resource. In many cases, we need to convert PHP arrays into URI format for passing to APIs or for use in web pages.

In this article, we will introduce how to convert PHP arrays into URI strings to achieve functions such as passing data, search queries, and more.

  1. Using http_build_query

Using the http_build_query function is the easiest way to convert a PHP array into URI format. This function converts a PHP array into a URI encoded string and returns this string:

<?php
$array = array(&#39;name&#39; => 'John Smith', 'age' => 34);
$queryString = http_build_query($array);

echo $queryString;

Output:

name=John+Smith&age=34

In the above example, we convert a PHP array containing name and age information Associative arrays are converted to URI format. Using the http_build_query function we can ensure that the result is a correct URI encoded string.

  1. Implement URLEncode yourself

If you don’t want to use the built-in function http_build_query, you can manually convert the array to URI format. In this case, we can use PHP's built-in urlencode function to encode the string in the URI:

<?php
$array = array(&#39;name&#39; => 'John Smith', 'age' => 34);
$queryString = '';

foreach($array as $key => $value){
    $queryString .= urlencode($key) . '=' . urlencode($value) . '&';
}

// 去掉结尾的 &
$queryString = rtrim($queryString, '&');

echo $queryString;

Output:

name=John+Smith&age=34

In the above example, we use foreach to loop through the array keys and values ​​in , and then encode them using the urlencode function. We added an equal sign and an ampersand between each key and value, and used the rtrim function to remove the ampersand at the end of the string.

  1. Use PHP_QUERY_RFC3986 formatting

In some cases, we need to use fax URI encoding, which is RFC 3986 format. If we want to use this format, we need to set the second parameter to PHP_QUERY_RFC3986 when calling the http_build_query function:

<?php
$array = array(&#39;name&#39; => 'John Smith', 'age' => 34);
$queryString = http_build_query($array, '', '&', PHP_QUERY_RFC3986);

echo $queryString;

Output:

name=John%20Smith&age=34

In the above example, we use the http_build_query function to The array is converted to a string in RFC 3986 URI-encoded format. We can see that spaces are encoded as and no plus sign is used. This is the method specified in RFC 3986.

Summary

In this article, we introduced three ways to convert PHP arrays to URIs. Using the http_build_query function is the simplest and most common approach, while implementing the function manually and using the RFC 3986 format provides more flexibility and control. Either way, we can use these techniques to generate correctly formatted URI strings to achieve our business purposes.

The above is the detailed content of How to convert PHP array to URI string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn