簡體   English   中英

PHP函數返回空

[英]php function returns empty

(首先,英語不是我的母語,所以請原諒詞匯或語法上的任何錯誤。其次,我不是專業程序員,我通過閱讀在線教程學習了PHP)

我寫了一個php函數,用html代碼替換unicode字符的子字符串(形式為\\ uXXXX)。 這是功能:

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 ;
    }
}

我稱這個函數如下:

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

但是,雖然函數中的“ echo”確實顯示了所需的結果,但該函數似乎返回了一個空(或null?)字符串

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

我不明白為什么。 對於專業的程序員來說,這可能是顯而易見的,但是我不是...

謝謝您的幫助。

現在您不從函數返回任何內容,更多地了解遞歸函數,只需在函數中添加return即可:

if (strpos($var, '\u') !== false) {
    return unicode_to_html($var);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM