Sabya
Sabya

Reputation: 11894

change default character set of PHP functions like "htmlspecialchars"

I am using PHP 5.2.6 and my app's character set is UTF-8.

Now, how should I change PHP's default character set? NOT the one which specifies output's mime time and character set.

But which will change for all the PHP function like htmlspecialchars, htmlentities, etc.

I know, there is a parameter in those functions which takes the character set of the input string. But I don't want to specify for all the functions I use. And if somewhere I forget, it will be mess.

I also know, that I can wrap those functions and create my own wrapper like:

function myHtmlize($str)
{
  return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
}

I also, don't like this solution.

I really want to tell PHP, that by default take 'UTF-8' as the character set. Not 'iso-8859-1'.

Is it possible?

Upvotes: 7

Views: 8292

Answers (4)

PHP Guru
PHP Guru

Reputation: 1558

From php.net:

5.4.0 The default value for the encoding parameter was changed to UTF-8.

In modern times you need just update your version of PHP to 5.4 or better and set the default_charset directive in php.ini to UTF-8 (which is already the default). You may also be able to do it programatially like this:

ini_set('default_charset', 'UTF-8');

Sources:

Upvotes: 0

troelskn
troelskn

Reputation: 117487

I'm not entirely sure, but I think mbstring.func_overload works with htmlentities.

htmlspecialchars is charset-neutral btw. (At least as long as the charset supports the ascii subset, which utf-8 does).

Upvotes: 1

VolkerK
VolkerK

Reputation: 96159

There is a C-function determine_charset(char *charset_hint ...) which is used to find the "right" charset based on

in that order and depending on whether some extensions are built-in or not.
The "problem" is, when you call htmlentities('xyz') this determine_charset() is called with charset_hint=NULL and the first this function does is:

/* Guarantee default behaviour for backwards compatibility */
if (charset_hint == NULL)
    return cs_8859_1;

You have to call at least htmlentities('xyz', ENT_QUOTES, '')

Upvotes: 2

Ahmet Kakıcı
Ahmet Kakıcı

Reputation: 6404

Like this one ? https://www.php.net/manual/en/function.setlocale.php

* LC_ALL for all of the below
* LC_COLLATE for string comparison, see strcoll()
* LC_CTYPE for character classification and conversion, for example strtoupper()
* LC_MONETARY for localeconv()
* LC_NUMERIC for decimal separator (See also localeconv())
* LC_TIME for date and time formatting with strftime()
* LC_MESSAGES for system responses (available if PHP was compiled with libintl)

Upvotes: 2

Related Questions