how can i get the "filesize" from a string in php?
I put the string in a mysql database as a blob and i need to store the size of the blob. My solution was to create a temp file and put the string into the temp file. now i can get the filesize from the "string". but that solution is not good...
greetings
It depends. If you have mbstring function overloading enabled, the only call that will work will be mb_strlen($string, '8bit');. If it's not enabled, strlen($string) will work fine as well.
So, you can handle both cases like this:
if (function_exists('mb_strlen')) {
$size = mb_strlen($string, '8bit');
} else {
$size = strlen($string);
}
SELECT length(field) FROM table
From the MySQL docs:
LENGTH(str)
Returns the length of the string str,
measured in bytes. A multi-byte
character counts as multiple bytes.
This means that for a string
containing five two-byte characters,
LENGTH() returns 10, whereas
CHAR_LENGTH() returns 5.
strlen()
before putting it into mysql, or in SQL:
LENGTH()
Notice that lenght can be various depending on character set. If you want to have real length in bytes use strlen(), if you want to have character count use mb_strlen() (if you have utf-8 encoding for example)
If all you are storing is the string, then the size should be the length of your string times the number of bytes in the charset. So for Unicode that would be 2*strlen($string).
strlen($string) is the best example for viewing the size(MB) of a string
strlen() doesnt actually return the number of elements in a string, but the number of bytes in it
example:
echo(strlen('a■')); will return 4, because the black square character is made of 3 bytes, and the 'a' character is made of one.
use mb_strlen() as then you can tell it what type of encoding the string uses (if any) to get the size of it in bytes.
Related
I need to count the number of such a textarea's value. That textarea might be containing 5000 characters. But I just need to know whether is the number of those characters more than 20 characters or not. I can do that by using strlen() function. Something like this:
$content = $_POST['textarea_content'];
$content_length = mb_strlen($content, 'utf8');
if ( $content_length > 20 ) {
// do stuff
}
But my approach isn't optimise at all. It counts the number of all characters and then compare it. As I said, sometimes there is lots of characters like 5000 characters. So is there any approach to break counting after 20 characters?
Strings in PHP have an internal variable that saves the length of the string, so runtime of strlen($str) is not depends on the length of the string at all.
Your problem is that you want to use mb_strlen in order to get the number of characters in the string (and not the number of bytes). In other words - you want to know the length of the string, even if the string contains Unicode characters.
If you know that your string is UTF-8, it can be used for optimization. UTF-8 will save at most 4-bytes per char, so if you use isset($str[80]) - you know for sure that your string is at-least 20 chars (and probably much more). If not, you will still have to use the mb_ functions to get the information you need.
The reason for the usage of isset instead of strlen is because you asked about the optimized way. You can read more in this question regarding the two.
To sum it up - your optimized code would probably be:
if (isset($str[80]) || mb_strlen(mb_substr($str, 0, 21, 'utf-8'), 'utf-8') > 20) {
....
}
In php, the code will first check the isset part, and if it return true the other part will not run (so you get the optimization here from both the isset and the fact that you don't need to run the mb_ functions).
If you have more information about the characters in your string you can use it for more optimization (if, for example, you know that your all of the chars in your string are from the lower range of the UTF-8, you don't have to use $str[80], you might as-well use $str[40].
You can use this table from wikipedia:
Together with the information from the utf8-chartable website:
In order to help optimize the number of bytes you might need for each char in your string.
I want to check the size of a string that can contain any type of data.
I have checked strlen and mb_strlen but I am unsure about the differences relating to different data contents.
Some background : what I need to do in the end is cut the string in chunks to serialize it and store it in chunks (being able to restore afterwards). Chunks always have the same size (32Kb) and contain a serialized object with data and the part of the string that I cut, so I need the exact size of the string to be able to do that.
From PHP's manual:
Note:
strlen() returns the number of bytes rather than the number of characters in a string.
By contrast, mb_strlen will take character encoding into consideration. It returns the number of actual characters as defined in the character encoding in the string. For multibyte/variable byte character encodings, strlen can/will be bigger than mb_strlen.
mb_strlen may also return FALSE if you specify a character encoding to which the string being tested doesn't conform.
I want to know the memory capacity of the single character in php.
Please anyone give me the code or reference link to get the memory of a character.
Is there any predefined function for calculating the memory of a character as like
strlen();
I have the sample code for calculating the bandwidth of the webpage
This is the sample code
$speed = 10;
ob_start();
include(filename.php);
$now = time();
foreach(str_split(ob_get_clean(), $speed*1024) as $chunk)
{
echo $chunk;
flush();
$now++;
while($now > time())
{
usleep(1000000);
}
}
From this i can get the number of character by using the ob_get_lenght().
If i know the memory of a character then i can find the bandwidth for the webpage
In PHP a character always maps one-to-one on a single octet, ie. a byte. As such strlen correctly returns the number of bytes in a single string - although it might consume more memory because of internal representation (trailing zero or length integer).
To support multibyte notations you need to use the mbstring functions.
See http://www.php.net/manual/en/language.types.string.php. A character is the same as a byte, according to that page.
Also, a character can be multiple bytes (multibyte), depending on the encoding. From my experience, you would need to have the mbstring extension enabled for that. If you don't know if your characters are multiple bytes, then each character is probably one byte.
In PHP what does it mean by a function being binary-safe ?
What makes them special and where are they typically used ?
It means the function will work correctly when you pass it arbitrary binary data (i.e. strings containing non-ASCII bytes and/or null bytes).
For example, a non-binary-safe function might be based on a C function which expects null-terminated strings, so if the string contains a null character, the function would ignore anything after it.
This is relevant because PHP does not cleanly separate string and binary data.
The other users already mentioned what binary safe means in general.
In PHP, the meaning is more specific, referring only to what Michael gives as an example.
All strings in PHP have a length associated, which are the number of bytes that compose it. When a function manipulates a string, it can either:
Rely on that length meta-data.
Rely on the string being null-terminated, i.e., that after the data that is actually part of the string, a byte with value 0 will appear.
It's also true that all string PHP variables manipulated by the engine are also null-terminated. The problem with functions that rely on 2., is that, if the string itself contains a byte with value 0, the function that's manipulating it will think the string has ended at that point and will ignore everything after that.
For instance, if PHP's strlen function worked like C standard library strlen, the result here would be wrong:
$str = "abc\x00abc";
echo strlen($str); //gives 7, not 3!
More examples:
<?php
$string1 = "Hello";
$string2 = "Hello\x00World";
// This function is NOT ! binary safe
echo strcoll($string1, $string2); // gives 0, strings are equal.
// This function is binary safe
echo strcmp($string1, $string2); // gives <0, $string1 is less than $string2.
?>
\x indicates hexadecimal notation. See: PHP strings
0x00 = NULL
0x04 = EOT (End of transmission)
ASCII table to see ASCII char list
Is there a native or inexpensive way to check for the length of a string in bytes in PHP?
See http://bytes.com/topic/php/answers/653733-binary-string-length
Relevant part:
"In PHP, like in C, the string ends with a zero-character, '\0', (char)
0, null-terminator, null-byte or whatever you like to call it."
No, that's not the case - PHP strings are stored with both the length and the
data, unlike C strings that just has one pointer and uses a terminator. They're
"binary-safe" - NUL doesn't terminate the string.
See the definition of zvalue_value in zend.h; the string part has both a "char
*val" and "int len".
Problems would start if you're using the mbstring.func_overload, which changes
how strlen() and the other functions work, and does try and treat strings as
strings of characters in a specific encoding rather than a string of bytes.
This is not the normal PHP behaviour.
The answer is that strlen should return the number of bytes regardless of the content of the string. For multi-byte character strings, you get the wrong number of characters, but the right number of bytes. However, you need to be certain you're not using the mbstring overload, which changes how strlen behaves.
In the event that you have mbstring overload set or your are developing for the platforms where you are unsure about this setting you can do the following:
$len=strlen(bin2hex($data))/2;
The reason why this works is that in Hex you are guaranteed to get 2 characters for all bytes that come from bin2hex (it returns two chars even for the initial binary 0).
Note that it will use significantly more resources than a normal strlen (afterall, so you should definitely not do that to the large amount of data if it's not absolutely necessary.
On php.org, someone was nice enough to create this function. Just multiply by 8 and you've got however many bits were in that string, as the function returns bytes.
The length of a string (textual data) is determined by the position of the NULL character which marks the end.
In case of binary data, NULL can be and often is in the middle of data.
You don't check the length of binary data. You have to know it beforehand. In your case, the length is 16 (bytes, not bits, if it is UUID).
As far as UUID validity is concerned, any 16-byte value is a valid UUID, so you are out of luck there.