What is a binary-safe function? And what are your applications?

6

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

  

Note:Thisfunctionisbinary-safe.

Igavea searched here on the net and found nothing about it.

What is a binary-safe function? And what are your applications?

    
asked by anonymous 16.01.2017 / 23:28

1 answer

7

It basically means that it will work with any bytes string.

If you put control characters, "\n" , or null character ""chr(0)"" , it will not have any problem replacing.

Example:

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

This works perfectly.

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

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

Note that this is just architectural decisions; it is the task of the programmer to know the language and resolve it as needed. Just if you need a safe string in C, just do as PHP does internally: storing the data size instead of specifying it with a specific character.

    
16.01.2017 / 23:35