A
A
Anton_volkov2019-02-05 12:20:12
PHP
Anton_volkov, 2019-02-05 12:20:12

How can I set the internal encoding to utf-8 in php 7.3 so that string functions (n: strlen) work correctly with multi-byte encodings?

here https://habr.com/ru/company/avito/blog/335584/#ust...
said:
"In php.7.2 strlen() will no longer return the length of the string in bytes, instead it will return the length in characters according to the currently selected internal encoding"
Now I don't know how to set the internal encoding
tried like this:

<?php
mb_internal_encoding("UTF-8"); 
$a = 'русский';
echo strlen($a);

but the output is 14
So how do I set this up?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2019-02-05
@misantron

perhaps you should consider using mb_strlen() instead of strlen() ?

N
neol, 2019-02-05
@neol

Your link is an explanation of how mbstring.func_overload works and why it's bad, not a change in the behavior of the strlen function. That is, starting from 7.2, the behavior of the strlen function becomes more predictable (because it does not depend on the configuration). And for multibyte encodings, the corresponding mb_* functions should be used.

A
Alexander, 2019-02-05
@Minifets

1) It says that the func_overload parameter is in php.ini.
If you want the strlen function to be overloaded, then in php.ini you need to specify:
2) Starting with version 7.2, this parameter is considered obsolete and must be strictly = 0, otherwise the code will throw notice. Those. you need to use the functions from the mbstring module directly, mb_strlen instead of strlen.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question