<?php

header ('Content-type: text/html; charset=utf-8');
$username = "Nicolas Martignoni";

echo 'This is the output of Moodle:<br />';

echo $realuserinfo.get_string('loggedinas', 'moodle', $username);

echo '<br /><br />';
echo 'This should appear like this:<br />';
echo 'Connecté sous le nom « Nicolas Martignoni »';
echo '<br /><br />';
echo 'If you replace the 2 non-breaking spaces of the string <i>Connecté sous le nom « $a »</i> with HTML entities (&amp;nbsp;), you get the correct output. ';
echo 'To workaround this issue, I have to code every non-breaking space in lang files as the entity.<br />';
echo 'WARNING: The first non-breaking space doesn\'t seem to cause problem. Only the second one (after the var $a) causes the issue.';


// Function get_string() from moodlelib.php mimicking the real one (all non
// relevant portions of code where deleted
function get_string($identifier, $module='', $a=NULL, $extralocations=NULL) {

    if ($result = get_string_from_file($identifier, $locallangfile, "\$resultstring")) {
        if (eval($result) === FALSE) {
            trigger_error('Lang error: '.$identifier.':'.$locallangfile, E_USER_NOTICE);
        }
        return $resultstring;
    }
}

// Function get_string_from_file() from moodlelib.php mimicking the real one.
// Simply returns the string that exhibits the problem
function get_string_from_file($identifier, $langfile, $destination) {

    // The following string is the string defined in fr_utf8/moodle.php
    // $string['loggedinas'] = 'Connecté sous le nom « $a »';
    // This string contains 2 non-breaking spaces and opening and closing
    // french guillemets. Try to replace the non-breaking spaces with normal
    // ones or with &nbsp; entities to get the correct output.
    $temp =  $destination .'= sprintf("Connecté sous le nom « $a »");';
    return preg_replace('/(\$a(->\w+)?)/', '{\\1}', $temp);

}

?>

