Why does PHP handle carriage return / Linefeed Combo as a single byte?

I have a php script that truncates a string of 41 bytes. I call strlen on a string to check its size. However, if the line has the command "\ r \ n", this combo is considered as one byte. Therefore, in my case, instead of 42 bytes, PHP considers it to be 41 bytes.

Also substr truncates it to 42 instead of 41 bytes.

if (strlen($value) > 41) { $value = substr($value, 0, 41); 

Another strange condition. I have a large dataset that I go through this function. Thousands of lines. If I use a simpler set of test data, then the code works correctly, treating "\ r \ n" as 2 bytes.

Any ideas? Thanks.

+7
source share
1 answer

convert combo \ r \ n to \ n, do whatever you need, then return everything \ n to combo ...

 str_replace("\r\n","\n",$value); if (strlen($value) > 41) { $value = substr($value, 0, 41); str_replace("\n","\r\n",$value); 

hope this works for you without knowing what you are trying to do

+1
source

All Articles