PHP: convert Base64URL to Base64 safely

TL;DR: Validate locally, fix the first real error, validate again (no upload).

Handle PHP: convert Base64URL to Base64 safely with a repeatable Base64URL workflow: normalize alphabet, fix padding, decode locally, and validate.

Base64URL quick map

RuleBase64Base64URL
Alphabet+, /-, _
PaddingOften has =Often omits =
Typical useGeneral binary transportURLs, JWT segments

Focus for this query

  • Padding is required for many decoders: length must be divisible by 4.

Safe decode workflow

  1. Remove whitespace/newlines.
  2. Normalize alphabet: -+, _/.
  3. Add padding so length % 4 == 0.
  4. Decode locally and validate output shape (JSON/text/binary).

Runtime snippet (generic)

function base64urlToBase64(input) {
  const s = String(input || '').replace(/\s+/g, '').replace(/-/g, '+').replace(/_/g, '/');
  return s + '='.repeat((4 - (s.length % 4)) % 4);
}

const b64 = base64urlToBase64(token);
const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
const text = new TextDecoder().decode(bytes);
console.log(text);

Frequent failures

Privacy guardrail

Base64/Base64URL strings can still contain secrets. Decode locally, redact sensitive fields, and only then share snippets.

FAQ

Is anything uploaded to a server? No. All processing happens locally in your browser. Files are never uploaded.

What is the recommended workflow? Validate the input, fix the first real issue, validate again, then export/convert. This avoids compounding errors.

Privacy & Security
All processing happens locally in your browser. Files are never uploaded.

Next pages to check

Closest crawled pages without impressions yet. Added to speed first-impression conversion.

neighbor csharp php json decode arg1 string array given workflows data importneighbor csharp php json decode arg1 string float given workflows data importneighbor csharp php json decode arg1 string int given workflows data importneighbor csharp php json decode expects string array given workflows data importneighbor csharp php json decode expects string array given workflows enterprise rolloutneighbor csharp php json decode expects string null given workflows data importneighbor csharp php json decode state mismatch invalid or malformed json workflows enterneighbor csharp php json encode type is not supported workflows data importneighbor php csharp newtonsoft error converting 123 system uri user id checklists webhooneighbor php csharp newtonsoft error converting null system net ipaddress value workflowneighbor php csharp stj could not be converted system uri token workflows multi tenantneighbor php go json cannot unmarshal array into field payload items type float64 troublneighbor php go json cannot unmarshal array into field payload items type int troubleshoneighbor php go json cannot unmarshal array into field payload user type string troublesneighbor php go json cannot unmarshal array into field user createdat type time time troneighbor php go json cannot unmarshal array into field user email type int64 troubleshooneighbor php go json cannot unmarshal array into field user id type float64 troubleshootneighbor php go json cannot unmarshal array into field user id type string troubleshooti

Quick fix checklist

  • Reproduce the error on a minimal input.
  • Check type/format and field mapping.
  • Apply the smallest safe fix.
  • Validate on production-like payload.