2

(First of all, English is not my native language, so please excuse any error in vocabulary or grammar... Second, I'm not a professionnal programmer, I learned PHP by reading online tutorials)

I wrote a php function to replace substrings of unicode characters (by the form \uXXXX) by their html code. Here's the function :

function unicode_to_html($var){
    if(isset($var) && !empty($var) ) 
    {
        $pos = strpos($var, '\u');
        $hexa_code = substr($var, $pos+2, 4) ;
        $unicode_string = '\u' . $hexa_code ;
        $deci_code = hexdec($hexa_code) ;
        $html_string = '&#' . $deci_code . ';' ;
        $var = str_replace($unicode_string,  $html_string, $var) ;
        if (strpos($var, '\u') !== false) {
            unicode_to_html($var);
        } else {
            $output = $var ;
            echo 'result of the function unicode_to_html : ' . $output . '<br />' ;
            try {
                return $output ;
            }
            catch (Exception $e) {
                echo 'Exception : ',  $e->getMessage(), "\n";
            }
        }
    } 
    else 
    {
        return $var ;
    }
}

I call this function as follows :

$var = 'Velibor \u010Coli\u0107'; 
echo 'input : ' . $var . '<br />' ;
$var2 = unicode_to_html($var) ;
echo 'output : ' . $var2 . '<br />' ;

but while the "echo" within the function does displays the wanted result, the function seems to return an empty (or null ?) string

input : Velibor \u010Coli\u0107
result of the function unicode_to_html : Velibor Čolić 
output :

and I don't see why. It may be something obvious for a professionnal programmer, but this I am not...

Thank you for your help.

hong4rc
  • 3,999
  • 4
  • 21
  • 40
  • 1
    you have no return in the block if `(strpos($var, '\u') !== false)` – Liora Haydont Jul 13 '18 at 15:57
  • Why not just use [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through/279279)? – CD001 Jul 13 '18 at 15:59
  • Excuse me, I don't get it. This is a recursive call to the function, until there is no more substrings to replace. So it seemed logical for me to have no return there... – Stan the man Jul 13 '18 at 15:59
  • this is example of bad code. why `return $var` is conditional (aside from potentially `$var` being not initialized under certain circumstances)? why multiple return points? – Marcin Orlowski Jul 13 '18 at 16:00
  • @Marcin Orlowski It is that bad ? I thought it was best to think of the possible errors in function calls... the only return point I wish to use is the `return $output` – Stan the man Jul 13 '18 at 16:09
  • This shouldn't be a recursive function really. It's best to use a while loop on the `if $var contains \u` and repeating the replacement instead. The culprit is indeed the lack of `return` on the inner `unicode_to_html($var);` call it seems. – mario Jul 13 '18 at 16:36

1 Answers1

0

Now you not returning anything from function, read more about recursive functions, just add return to function:

if (strpos($var, '\u') !== false) {
    return unicode_to_html($var);
}
RainDev
  • 1,098
  • 8
  • 9
  • OK, that was my error. My mistake was to think that if the echo is OK in the function, il involves that the path until this was correct. Thank you very much ! Thank you Liora Haydont too, you did say the same thing but I didn't understand... – Stan the man Jul 13 '18 at 19:37