How can I replace the "+" plus sign with its corresponding url encoding of "%2B"?

How can I replace the "+" plus sign with its corresponding url encoding of "%2B"?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The + vs %2B Confusion in URL Encoding

You are encountering a classic misunderstanding of what URL encoding actually does, rather than a bug in your logic or a limitation of PHP's urlencode() function. The core issue lies in the fundamental definition of the plus sign within the query string context. When you run $string = urlencode($string); on a string containing spaces, urlencode() correctly encodes that space as %20. However, many frameworks and servers historically treat a literal + character in a query string as a shorthand for a space. This is an older convention from the early days of HTML forms where sending a space required a specific byte sequence, and developers found it convenient to use ASCII 43 (+) instead of the two-byte %20.

When you subsequently run urldecode() on a string that contains a +, the function interprets that character as a space. This is why your browser displays plus( ) with a gap between the parentheses instead of the intended parenthesis characters. The root cause is not that urlencode() failed to encode the space, but that the resulting string structure relies on the legacy rule that + equals space.

To achieve your goal of ensuring the literal plus sign appears as %2B, you must understand the order of operations in URL construction. If you are manually constructing a URL or processing data that will be placed into the query component, you should not rely solely on urlencode() followed by a manual string replacement of + to %2B. Doing so creates an inconsistent state where your decoder cannot distinguish between a space (which was encoded as %20) and a plus sign that happened to be replaced.

The correct approach depends entirely on whether you are handling form data or arbitrary user input intended for display. If the input is coming from a standard HTML form field, urlencode() is generally sufficient because it encodes the space as %20. The browser's legacy behavior of treating + as a space only applies when parsing incoming request data, not when generating outgoing encoded strings that strictly follow RFC 3986.

However, if you need to preserve a literal + sign in your output and ensure it never gets interpreted as a space by any downstream decoder, you must explicitly encode the plus sign before placing it into the URL query string. The safest method is to use rawurlencode() or to manually escape the character using addcslashes() before passing it to the encoding function.

Here is a practical implementation pattern in PHP:

$input = 'plus(+)';

// Method 1: Using rawurlencode for strict RFC 3986 compliance
// This encodes spaces as + and plus signs as %2B, but we can handle spaces differently if needed.
// However, for a simple replacement strategy that guarantees no ambiguity:

$input = addcslashes($input, '+'); // Escapes the + to \+
$encoded = urlencode($input);      // This will now encode \+ as %5C%2B or handle it based on context.

// A more robust approach for this specific case:
// Replace the literal '+' with '%2B' ONLY if you are constructing a string manually 
// and have already handled spaces (e.g., replaced space with %20).

$temp = str_replace(' ', '%20', $input); // Ensure spaces are safe first
$new_string = str_replace('+', '%2B', $temp);

$final_url = 'https://example.com?data=' . rawurlencode($new_string);

In the code above, the critical step is ensuring that the space character is handled before or alongside the plus sign. If you simply replace + with %2B on a string where spaces are still present as characters (or encoded as %20), you might end up with a mix of legacy and strict encoding that confuses decoders.

If you are working within the Laravel ecosystem, the framework provides helpers that abstract these complexities away. Using url() or form request classes ensures that query parameters are consistently encoded according to modern standards without requiring manual string manipulation. You can rely on app()->request->getQueryString() to retrieve data that has already been safely decoded, meaning you don't need to manually reverse the encoding unless you are building a custom serialization format.

The trade-off here is performance versus correctness. Using addcslashes or multiple str_replace calls adds negligible overhead compared to the risk of generating malformed URLs. The most common mistake developers make is assuming that urlencode() output is always safe for display without verifying how the specific browser or server decodes it. By explicitly converting + to %2B only after ensuring spaces are encoded as %20, you create a URL that adheres strictly to the standard where + means nothing but itself, preventing the space interpretation bug entirely.

Our network