strlen not checking for NULL

The rational behind it is simple — how can you check the length of something that does not exist? Also, unlike “managed languages” there is no expectations the run time system will handle invalid data or data structures correctly. (This type of issue is exactly why more “modern” languages are more popular for non-computation or … Read more

Sizeof vs Strlen

sizeof and strlen() do different things. In this case, your declaration char string[] = “october”; is the same as char string[8] = “october”; so the compiler can tell that the size of string is 8. It does this at compilation time. However, strlen() counts the number of characters in the string at run time. So, … Read more

why is -1>strlen(t) true in C? [duplicate]

Anyone got an idea what i’m doing wrong strlen() returns a size_t, which is an unsigned integer type. -1 interpreted as an unsigned integer is a large value, so it ends up being greater than the length of your string. You can use the -Wsign-compare flag in gcc and some other compilers to warn you … Read more

strlen() and UTF-8 encoding

how about using mb_strlen() ? http://lt.php.net/manual/en/function.mb-strlen.php But if you need to use strlen, its possible to configure your webserver by setting mbstring.func_overload directive to 2, so it will automatically replace using of strlen to mb_strlen in your scripts.

How does the strlen function work internally?

strlen usually works by counting the characters in a string until a \0 character is found. A canonical implementation would be: size_t strlen (char *str) { size_t len = 0; while (*str != ‘\0’) { str++; len++; } return len; } As for possible inherent bugs in the function, there are none – it works … Read more

Add … if string is too long PHP [duplicate]

The PHP way of doing this is simple: $out = strlen($in) > 50 ? substr($in,0,50).”…” : $in; But you can achieve a much nicer effect with this CSS: .ellipsis { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } Now, assuming the element has a fixed width, the browser will automatically break off and add the … … Read more