Nis Bornoe - Bornoe.org

PHP: Danish letters "æ, ø & å"

Page last updated: September 8, 2016

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 with the HTML code: <meta charset="utf-8">

HTML codes for æ, ø & å

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