To convert UTF-8 characters with macrons (e.g., Māori macrons like “ā”, “ē”, etc.) into their corresponding HTML entity codes in PHP, you can use the mb_encode_numericentity() function from the mbstring extension.
Here’s an example of how to achieve this:
Example Code:
<?php
// String containing UTF-8 characters with macrons
$string = "Tēnā koe, ngā mihi!";
// Define the conversion map for all UTF-8 characters
$convmap = array(0x0100, 0x017F, 0, 0xFFFF);
// Convert UTF-8 characters to HTML entities
$encoded_string = mb_encode_numericentity($string, $convmap, "UTF-8");
// Output the result
echo $encoded_string;
?>Explanation:
$string: This is the input string containing UTF-8 macron characters.$convmap: This is a conversion map that defines the range of characters to be converted. In this example, the range0x0100to0x017Fincludes the macron characters. The range can be adjusted depending on the characters you need to convert.mb_encode_numericentity(): Converts the characters in the specified range to numeric HTML entities. This function requires thembstringextension to be enabled in your PHP installation."UTF-8": Specifies the encoding of the input string.
Example Input/Output:
- Input: 
"Tēnā koe, ngā mihi!" - Output: 
"Tēnā koe, ngā mihi!" 
Notes:
- The function converts only the characters within the specified range in the 
$convmap. You can expand or modify the range if you need to convert other special characters as well. - The 
mbstringextension must be enabled in your PHP environment. You can check if it’s enabled usingphpinfo(). 
This approach will correctly convert the UTF-8 macron characters to their corresponding HTML entity codes.
 ![]()