Is mb_ * required to replace single-byte characters from a multi-byte string?

Say I have UTF-8 text:

 âàêíóôõ <br> âàêíóôõ <br> âàêíóôõ 

I want to replace <br> with <br /> . Do I need to use mb_str_replace or can I use str_replace ?

Konsonirovanie < b r / > - all single-byte char?

+6
source share
2 answers

Since str_replace is binary safe and UTF-8 is bijective encoding, you can use str_replace even if the search string or replacement contains multi-byte characters if all three parameters are encoded as UTF-8.

In this case, there is no mb_str_replace function.

If your encoding is not bijective, i.e. there are several representations of the same line, for example < in UTF-7, which can be expressed as '+ADw-' and '<' , you need to convert all the lines to the same (bijective) encoding, apply str_replace , and then convert strings in the target encoding.

+4
source

Link for secure UTF-8 string management in PHP . There is no hard and fast rule. Some built-in functions of PHP string functions can work safely on utf-8, some can be used with caution, and some cannot.

No mb_str_replace() . Check out the UTF-8 Safe Functionality section: explode() and str_replace() are safe if all three arguments to it are valid UTF-8 strings.

+2
source

Source: https://habr.com/ru/post/907793/


All Articles