Question

I heard that in PHP there are some alternatives for the functions substr() and strlen() which handles safer bits. Is this true and if it is then what are those functions? I heard that it is based on the function strcmp() but I don't see directly how can I use it.

Was it helpful?

Solution

The functions are prefixed with mb_. See here: http://php.net/manual/en/ref.mbstring.php

From PHP's Multibyte String extension's introduction:

mbstring provides multibyte specific string functions that help you deal with multibyte encodings in PHP. In addition to that, mbstring handles character encoding conversion between the possible encoding pairs. mbstring is designed to handle Unicode-based encodings such as UTF-8 and UCS-2 and many single-byte encodings for convenience.

OTHER TIPS

Maybe you mean safe for multibyte character encoding like UTF-8. Because substr and strlen are not safe for multibyte encodings. They count each character as one byte. But the multibyte string function take that into account.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top