Signature
convert_uudecode(string $string): string|falsePurpose
convert_uudecode() reverses uuencoding. Uuencoding is an old scheme that turns arbitrary bytes into printable ASCII characters so they can travel through channels that only handle text, such as early email and Usenet. The function takes a uuencoded string and gives you back the original bytes. It is the counterpart of convert_uuencode().
You will rarely choose uuencode for new designs; Base64 is far more common. The function is still useful when you read legacy data, test an old interchange format, or need to reverse a value produced by convert_uuencode().
Signature
Parameters and defaults
$string: the uuencoded data. It is required and has no default value.
The data must be the encoded body only. The manual notes that the function does not accept the begin line or the end line that wrap a uuencoded file. If you have a complete uuencoded file in memory, remove those two lines first and pass only the lines in between.
Each line of uuencoded data begins with a character that encodes how many original bytes the line holds, followed by the encoded characters. A line holds at most 45 original bytes, so longer input is split across several lines. The encoder ends the data with a line containing a single backtick.
Return value and failure cases
The function returns the decoded data as a string. On failure it returns false. In practice, failure means the input is not a valid uuencoded string. Because the return type is string|false, compare the result with false using === before you trust it:
- An empty decoded string and
falseare different things, so a loose check such asif (!$result)can mislead you. - The decoded value is a byte string. It can contain NUL bytes, newlines or non-UTF-8 bytes. Do not assume it is printable text.
Example 1: the manual's sample
The first example decodes a small literal. The leading + encodes a length of 11 bytes, and the rest of the line carries the characters of I love PHP!. A final line with a lone backtick marks the end of the data.
echo convert_uudecode("+22!L;W9E(%!(4\"$`\n`"), "\n";I love PHP!
Notice that the literal sits in a double-quoted PHP string, so \" is a quote and \n is a newline. If you typed the same data in single quotes, the \n would stay as two characters and the data would be corrupted.
Example 2: a round trip with newlines
The safest way to see the function work is to encode something and decode it again. This example uses text that contains line breaks and confirms that the bytes survive unchanged.
$original = "Line 1\nLine 2\n";
$encoded = convert_uuencode($original);
$decoded = convert_uudecode($encoded);
echo json_encode(['same' => $decoded === $original, 'length' => strlen($decoded)], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"same":true,"length":14}
The strict comparison proves that the line breaks inside the data are not confused with the line structure of the encoded form.
Example 3: data longer than one encoded line
Because one encoded line carries at most 45 original bytes, a 120-byte string needs several lines. The decoder joins them again without any extra work from you.
$original = str_repeat('PHP ', 30);
$encoded = convert_uuencode($original);
$decoded = convert_uudecode($encoded);
echo json_encode(['same' => $decoded === $original, 'length' => strlen($decoded)], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"same":true,"length":120}
Edge cases
- Begin and end lines. A pasted uuencoded file usually starts with something like
begin 644 nameand ends withend. These are not part of the encoded body, so strip them before decoding. - Binary content. Uuencoding exists to carry binary data, so the result may not be text. Use
strlen()rather than character-oriented functions when you inspect it. - Whitespace changes. Mail systems and editors sometimes alter trailing spaces or line endings. Altered lines can make data invalid, which is one reason Base64 replaced uuencode.
- Quoting in source code. Backticks, backslashes and
$characters are common in uuencoded text. Embed such literals carefully, or better, keep them in data rather than in code.
Common mistakes
- Passing a whole file. Including the
beginandendlines does not work, because the function does not accept them. - Using the wrong encoding family. Uuencoded text is not Base64 or quoted-printable. Use
base64_decode()for Base64 andquoted_printable_decode()for quoted-printable. - Ignoring
false. Always test with=== falsewhen the input comes from outside your program. - Treating the output as UTF-8. The decoded bytes are exactly what was encoded, whatever their encoding.
- Expecting encryption. Uuencoding is not secret or secure. Anyone can decode it.
Related functions
convert_uuencode()produces the encoded form that this function reads.base64_encode()andbase64_decode()are the modern alternative for text-safe binary data.quoted_printable_decode()handles another legacy mail encoding.bin2hex()andhex2bin()give a simple hexadecimal representation.
Recap
convert_uudecode(string $string): string|false decodes the body of uuencoded data and returns the original bytes, or false if the data is invalid. Remove any begin and end lines first, compare the result strictly with false, and remember that the output is a raw byte string. Round-tripping with convert_uuencode() is the easiest way to test your understanding.
Tested examples
echo convert_uudecode("+22!L;W9E(%!(4\"$`\n`"), "\n";I love PHP!
$original = "Line 1\nLine 2\n";
$encoded = convert_uuencode($original);
$decoded = convert_uudecode($encoded);
echo json_encode(['same' => $decoded === $original, 'length' => strlen($decoded)], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"same":true,"length":14}
$original = str_repeat('PHP ', 30);
$encoded = convert_uuencode($original);
$decoded = convert_uudecode($encoded);
echo json_encode(['same' => $decoded === $original, 'length' => strlen($decoded)], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"same":true,"length":120}