W3docs

How can I properly URL encode a string in PHP?

To URL encode a string in PHP, you can use the urlencode function.

To URL encode a string in PHP, you can use the urlencode function. This function will encode any special characters in the string, making it safe to use in a URL.

For example:

How to properly URL encode a string in PHP?

php— editable, runs on the server

This will output:


Hello%2C+World%21

You can also use the rawurlencode function, which works the same way as urlencode, but also encodes characters that are not allowed in URLs, such as spaces.

For example:

Example of using the rawurlencode() function to URL encode a string in PHP

php— editable, runs on the server

This will output:


Hello%2C%20World%21