tags:

views:

77

answers:

3

I have an XML ISO-8859-1 page, in which I have to output symbols like é.
If I output é it errors out. é works just fine.
So, what PHP function should I use to transform é to é

I can't move to utf-8 (as I assume some will suggest and rightfully so) This is a huge, legacy code.

+2  A: 

var_dump(ord('é'));

Gives

int(233)

Perhaps, you could use

print '&#' . ord('é') . ';';
Adam
That **could** have been a good solution if that was the only input I have. What I actually get is a huge text with some "strange" characters in it, not all of it.
Itay Moav
+4  A: 

Use mb_convert_encoding:

mb_convert_encoding("é", "HTML-ENTITIES", "ISO-8859-1");

gives ‚.

This example does not require that you enter the "é", which you may or may not be doing in ISO-8859-1:

mb_convert_encoding(chr(130), "HTML-ENTITIES", "ISO-8859-1");
Artefacto
It gives me a completely different result on my system.
Itay Moav
@Itay Moav It depends on how you're entering the "é". I'll update the answer.
Artefacto
+1  A: 

Try have a look in the comments here; http://php.net/manual/en/function.htmlentities.php

phil at lavin dot me dot uk
08-Apr-2010 03:34 

The following will make a string completely safe for XML:

<?php
function philsXMLClean($strin) {
    $strout = null;

    for ($i = 0; $i < strlen($strin); $i++) {
            $ord = ord($strin[$i]);

            if (($ord > 0 && $ord < 32) || ($ord >= 127)) {
                    $strout .= "&amp;#{$ord};";
            }
            else {
                    switch ($strin[$i]) {
                            case '<':
                                    $strout .= '&lt;';
                                    break;
                            case '>':
                                    $strout .= '&gt;';
                                    break;
                            case '&':
                                    $strout .= '&amp;';
                                    break;
                            case '"':
                                    $strout .= '&quot;';
                                    break;
                            default:
                                    $strout .= $strin[$i];
                    }
            }
    }

    return $strout;
}
?> 

All credits go to phil at lavin do me dot uk

Phliplip
crude but working - Thanks!
Itay Moav