What is a Binary-safe function? And what are its applications?

Asked

Viewed 518 times

6

I was browsing through the php.net when I came across the following note on the function str_replace:

inserir a descrição da imagem aqui

Note: This function is Binary-safe.

I gave a researched here on the net and found nothing about.

What is a Binary-safe function? And what are its applications?

1 answer

7


Basically it means that it will work with any sequence of bytes.

If you put control characters, "\n", or null character "\0", she won’t have any trouble making the replacement.

Example:

$str1 = 'quebra'.chr(0).'de'.chr(0).'linha';
$str2 = str_replace( chr(0), "\n", $str1 );

This works perfectly.

In PHP in general the functions are, because internally PHP associates the data to a kind of internal "variable" that stores the size.

In some languages for example, instead of the size being stored, what indicates the end of the string is the null character "\0" (which in PHP is returned by chr(0)), what would be an example of "no Binary safe".

Note that these are just architectural decisions, it is the task of the programmer to know the language and solve as needed. Simply if you need a string Binary safe in C, just do as PHP does internally: storing the size of the data, instead of indicating with a specific character.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.