6

These past few days I've been working toward converting my PHP code base from latin1 to UTF-8. I've read the two main solutions are to either replace the single byte functions with the built in multibyte functions, or set the mbstring.func_overload value in the php.ini file.

But then I came across this thread on stack overflow, where the post by thomasrutter seems to indicate that the multibyte functions aren't actually necessary for UTF-8, as long as the script and string literals are encoded in UTF-8.

I haven't found any other evidence whether this is true or not, and if it turns out I don't need to convert my code to the mb_functions then that would be a real time saver! Anyone able to shed some light on this?

Community
  • 1
  • 1
Spoonface
  • 1,513
  • 1
  • 20
  • 29

8 Answers8

11

As far as I understand the issue, as long as all your data is 100% in utf-8 - and that means user input, database, and also the encoding of the PHP files themselves if you have special characters in them - this is true true for search and comparison operations. As @ntd points out, a non-multibyte strlen() will produce wrong results when run on a string that contains multibyte characters.

THis is a great article on the basics of encoding.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
4

They aren't "necessary" unless you're using any of the functions they replace (and it's likely that you are using at least one of these) or otherwise explicitly need a feature of the extension such as HTTP handling.

When working towards UTF-8 compliance, I always fall back to the PHP UTF-8 Cheatsheet with one addition: PCRE patterns need to be updated to use the u modifier.

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
3

As soon as you're examining or modifying a multibyte string, you need to use a mb_* function. A very quick example which demonstrates why:

$str = "abcžđščćöçefg";
mb_internal_encoding("UTF-8");

echo "strlen: ".strlen($str)."\n";
echo "mb_strlen: ".mb_strlen($str)."\n";

This prints out:

strlen: 20
mb_strlen: 13
Aleksander Kmetec
  • 3,287
  • 2
  • 24
  • 19
2

thomasrutter indicates that the search does not need special handling. For example, if you need to check the length of an UTF8 string, I don't see how you can do that using plain strlen().

ntd
  • 7,372
  • 1
  • 27
  • 44
2

Functions such as mb_strtoupper may be necessary, too. strtoupper won't convert á to Á.

TRiG
  • 10,148
  • 7
  • 57
  • 107
1

There are a number of functions that expect strings to be single byte (And some even presume that it is iso-8859-1). In these cases, you need to be aware of what you're doing and possibly use replacement functions. There is a fairly comprehensive list at: http://www.phpwact.org/php/i18n/utf-8

troelskn
  • 115,121
  • 27
  • 131
  • 155
0

You could use the mbfunctions library that extends the multibyte functions in PHP:

http://code.google.com/p/mbfunctions/

Ignacio Lago
  • 2,432
  • 1
  • 27
  • 33
-1

You can use this http://php.net/manual/en/mbstring.overload.php setting in php.ini file, so you don't need to change you code.

But be careful, because not all string function will be automatically changed. This is one: http://php.net/manual/en/function.substr-replace.php

AnzeR
  • 590
  • 4
  • 11
  • 23
  • Not anymore: "This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged." – Oliver N. Sep 22 '21 at 08:23