user9098366
user9098366

Reputation:

addcslashes(), charlist parameter - Why certain characters get converted?

addcslashes($str, $charlist)

From http://php.net/manual/en/function.addcslashes.php

"If charlist contains characters \n, \r etc., they are converted in C-like style, while other non-alphanumeric characters with ASCII codes lower than 32 and higher than 126 converted to octal representation."

1) So this would be \0, \a, \b, \f, \n, \r, \t, \v as far as the first part. What does C-like style conversion mean (any url that I could check on the Net), and why does the conversion take place?

2) Why the "other non-alphanumeric characters with ASCII codes lower than 32 and higher than 126" are converted to their octal representation?

Upvotes: 1

Views: 290

Answers (1)

Barmar
Barmar

Reputation: 780994

If you do:

echo addcslashes("abc\nfoo\1bar", "\n\1")

the output is:

abc\nfoo\001bar

The purpose is to return the string in the format that C and PHP programmers are likely to write the literal. We usually write \n rather than \012 for newline, \e rather than \033 for Escape, \r rather than \015 for carriage return, etc, so that's what's returned. But most other non-printing characters don't have short escape sequences, so they're returned with octal escapes.

Upvotes: 1

Related Questions