PHP's iconv function

On a recent project, I found myself working on a tool to "preview" email. The idea is that once you have created your email, you want to see what it will look like. While running tests utilizing non US-ASCII characters, I ran into some rendering issues. The window itself was set to display using a charset of UTF-8, but the HTML version of the email was rendering as ISO-8859-1 once converted to text.

I believe that the real issue was that our development database was not set to accept UTF-8 characters. However, not wanting to run into a similar issue in the production environment, I went looking for a way to convert the text. What I found was PHP's iconv(). This did the trick.

Here's an example of how you might use it:

// Some Spanish to play with.
$strIn = "comenzó a cantar desde niña en la estación de radio local y en un programa de televisión";

// First let's convert the value of $strIn to the target encoding
$strOut = iconv("ISO-8859-1", "UTF-8", $strIn);

// iconv() returns FALSE if unable to translate
if($strOut == FALSE) {

// Could not convert, so set $strOut to $strIn
$strOut = $strIn;


echo "<div>".$strOut."</div>";

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.