The Homepage of Nis Bornoe

PHP: Danish letters "æ, ø & å"

Page updated: July 4, 2018

A common problem when accepting input containing the Danish letters "æ, ø & å" is converting them to the proper HTML codes. The following function uses a very simple approach. The function simply takes in a string, uses regular expressions to replace the letters with the proper HTML code and then returns the new string. This function can obviously be extended into replacing other special characters as well.

function replcSpecChar($string){
	$string = preg_replace("æ", "æ", $string);
	$string = preg_replace("ø", "ø", $string);
	$string = preg_replace("å", "å", $string);
	$string = preg_replace("Æ", "Æ", $string);
	$string = preg_replace("Ø", "Ø", $string);
	$string = preg_replace("Å", "Å", $string);
	return $string;

On a sidenote, in HTML5 the default character encoding is UTF-8. This can be included in the <head> section with the code: <meta charset="utf-8">

HTML codes for æ, ø & å

æ: &aelig;
ø: &oslash;
å: &aring;
Æ: &AElig;
Ø: &Oslash;
Å: &Aring;