/**
 * Takes the first UTF-8 character in a string and returns its Unicode codepoint.
 * @param string $utf8_character	The UTF-8 encoded character.
 * @return int						Returns: the codepoint; or 0xFFFD (unknown character) when the input string is empty.
 * This is a UTF-8 aware version of the function ord().
 * @link http://php.net/manual/en/function.ord.php
 * Note about a difference with the original funtion ord(): ord('') returns 0.
 */
function _api_utf8_ord($utf8_character)
{
    if ($utf8_character == '') {
        return 0xfffd;
    }
    $codepoints = _api_utf8_to_unicode($utf8_character);
    return $codepoints[0];
}
/**
 * Replaces text within a portion of a string.
 * @param string $string				The input string.
 * @param string $replacement			The replacement string.
 * @param int $start					The position from which replacing will begin.
 * Notes:
 * If $start is positive, the replacing will begin at the $start'th offset into the string.
 * If $start is negative, the replacing will begin at the $start'th character from the end of the string.
 * @param int $length (optional)		The position where replacing will end.
 * Notes:
 * If given and is positive, it represents the length of the portion of the string which is to be replaced.
 * If it is negative, it represents the number of characters from the end of string at which to stop replacing.
 * If it is not given, then it will default to api_strlen($string); i.e. end the replacing at the end of string.
 * If $length is zero, then this function will have the effect of inserting replacement into the string at the given start offset.
 * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
 * @return string						The result string is returned.
 * This function is aimed at replacing the function substr_replace() for human-language strings.
 * @link http://php.net/manual/function.substr-replace
 */
function api_substr_replace($string, $replacement, $start, $length = null, $encoding = null)
{
    if (empty($encoding)) {
        $encoding = _api_mb_internal_encoding();
    }
    if (_api_is_single_byte_encoding($encoding)) {
        if (is_null($length)) {
            return substr_replace($string, $replacement, $start);
        }
        return substr_replace($string, $replacement, $start, $length);
    }
    if (api_is_encoding_supported($encoding)) {
        if (is_null($length)) {
            $length = api_strlen($string);
        }
        if (!api_is_utf8($encoding)) {
            $string = api_utf8_encode($string, $encoding);
            $replacement = api_utf8_encode($replacement, $encoding);
        }
        $string = _api_utf8_to_unicode($string);
        array_splice($string, $start, $length, _api_utf8_to_unicode($replacement));
        $string = _api_utf8_from_unicode($string);
        if (!api_is_utf8($encoding)) {
            $string = api_utf8_decode($string, $encoding);
        }
        return $string;
    }
    if (is_null($length)) {
        return substr_replace($string, $replacement, $start);
    }
    return substr_replace($string, $replacement, $start, $length);
}