3

I have a very strange output from base64_decode function. The output is a string with length of 18, however when I var_dump the string .. the length is 32! This affects the preg_match function that I'm applying for this string. Any idea why?

$input_line = base64_decode('OQA3ADgAMAA5ADgAMgA3ADQAMwA2ADAANwA5ADAAMAAwADAA');
var_dump($input_line);

Output

string(36) "978098274360790000"

Regards

2
  • 1
    There's probably unprintable characters in there, e.g. nuls. if you iterate over each character of $input_line and print out the ascii value of each char, you'll find where they're hiding.. probably at the end of the string. Commented Oct 15, 2013 at 20:47
  • 1
    If you force a datatype then does vardump still produce the same result? var_dump($input_line(int)); Commented Oct 15, 2013 at 20:47

2 Answers 2

4

The string has a lot of non-printable characters (the NUL byte). You can see this by piping the value through a program like xxd:

0000000: 3900 3700 3800 3000 3900 3800 3200 3700  9.7.8.0.9.8.2.7.
0000010: 3400 3300 3600 3000 3700 3900 3000 3000  4.3.6.0.7.9.0.0.
0000020: 3000 3000                                0.0.
Sign up to request clarification or add additional context in comments.

Comments

1

Thank you everyone especially @tim-cooper There are null bytes in the string and I found the solution here: How can I remove the NULL character from string

so basically:

str_replace("\0", "", $input_line);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.