Reputation: 551
I have a php script that truncates a string at 41 bytes. I call strlen on the string to check its size. However, if the string has a "\r\n" combo, this combo is treated as one byte. So in my case instead of 42 bytes, PHP thinks it is 41 bytes.
Also substr truncates it to 42 instead of 41 bytes.
if (strlen($value) > 41)
{
$value = substr($value, 0, 41);
Another weird condition. I have a large set of data I am passing through this function. Thousands of strings. If I use a simpler test data set then the code works correctly, treating "\r\n" as 2 bytes.
Any ideas? Thanks.
Upvotes: 7
Views: 2494
Reputation: 4773
convert the combo \r\n to \n , do whatever u need , then revert all \n's to the combo ...
str_replace("\r\n","\n",$value);
if (strlen($value) > 41)
{
$value = substr($value, 0, 41);
str_replace("\n","\r\n",$value);
hope this will work for you not knowing what are you trying to do
Upvotes: 1