note 54906 modified in function.strlen by thiago

From: Date: Wed, 11 Feb 2009 14:04:23 +0000
Subject: note 54906 modified in function.strlen by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150481@lists.php.net to get a copy of this message
> Just a precisation, maybe obvious, about the strlen() behaviour: > with binary strings (i.e. returned by the pack() finction) is made > a byte count... so strlen returns the number of bytes contained > in the binary string. This is not always true. strlen() might be shadowed by mb_strlen(). If that is the case it might treat binary data as unocode string and return wrong value (I just found it out after fighting with egroupware email attachment handling bug). So, if your data is binary I would suggest using somthing like this (parts of the code from egroupware): <?php $has_mbstring = extension_loaded('mbstring') ||@dl(PHP_SHLIB_PREFIX.'mbstring.'.PHP_SHLIB_SUFFIX); $has_mb_shadow = (int) ini_get('mbstring.func_overload'); if ($has_mbstring && ($has_mb_shadow & 2) ) { $size = mb_strlen($this->output_data,'latin1'); } else { $size = strlen($this->output_data); } ?> -- Bartek --was-- > Just a precisation, maybe obvious, about the strlen() behaviour: > with binary strings (i.e. returned by the pack() finction) is made > a byte count... so strlen returns the number of bytes contained > in the binary string. This is not always true. strlen() might be shadowed by mb_strlen(). If that is the case it might treat binary data as unocode string and return wrong value (I just found it out after fighting with egroupware email attachment handling bug). So, if your data is binary I would suggest using somthing like this (parts of the code from egroupware): $has_mbstring = extension_loaded('mbstring') ||@dl(PHP_SHLIB_PREFIX.'mbstring.'.PHP_SHLIB_SUFFIX); $has_mb_shadow = (int) ini_get('mbstring.func_overload'); if ($has_mbstring && ($has_mb_shadow & 2) ) { $size = mb_strlen($this->output_data,'latin1'); } else { $size = strlen($this->output_data); } -- Bartek http://php.net/manual/en/function.strlen.php

« previous php.notes (#150481) next »