Q:

Alternative for htmlentities($string,ENT_SUBSTITUTE)

I got a bit of a stupid question;
Currently I am making a website for a company on a server which actually has a bit an outdated PHP version (5.2.17). I have a database in which many fields are varchar with characters like 'é ä è ê' and so on, which I have to display in an HTML page.
So as the version of PHP is outdated (and I am not allowed to updated it because there are parts of the site that must keep working and to whom I have no acces to edit them) I can't use the htmlentities function with the ENT_SUBSTITUTE argument, because it was only added after version 5.4.
So my question is: 

Does there exist an alternative to
  htmlentities($string,ENT_SUBSTITUTE); or do I have to write a function
  myself with all kinds of strange characters, which would be incomplete anyway.

A:

Define a function for handling ill-formed byte sequences and call the function before passing the string to htmlentties. There are various way to define the function.
At first, try UConverter::transcode if you don't use Windows.
http://pecl.php.net/package/intl
If you are willing to handle bytes directly, see my previous answer.
https://stackoverflow.com/a/13695364/531320
The last option is to develop PHP extension. Thanks to php_next_utf8_char, it's not hard.
Here is code sample. The name "scrub" comes from Ruby 2.1 (see Equivalent of Iconv.conv("UTF-8//IGNORE",...) in Ruby 1.9.X?)
// header file
// PHP_FUNCTION(utf8_scrub);

#include "ext/standard/html.h"
#include "ext/standard/php_smart_str.h"

const zend_function_entry utf8_string_functions[] = {
    PHP_FE(utf8_scrub, NULL)
    PHP_FE_END
};

PHP_FUNCTION(utf8_scrub)
{
    char *str = NULL;
    int len, status;
    size_t pos = 0, old_pos;
    unsigned int code_point;
    smart_str buf = {0};

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) == FAILURE) {
        return;
    }

    while (pos < len) {

        old_pos = pos;
        code_point = php_next_utf8_char((const unsigned char *) str, len, &pos, &status);

        if (status == FAILURE) {

            smart_str_appendl(&buf, "\xEF\xBF\xBD", 3);

        } else {

            smart_str_appendl(&buf, str + old_pos, pos - old_pos);

        }

    }

    smart_str_0(&buf);
    RETURN_STRINGL(buf.c, buf.len, 0);
    smart_str_free(&buf);
}

