2012-05-08 33 views
1

我試圖在HTML中顯示「<」/「>」作爲字符串而不是標籤。輸出「<"/">」HTML中的字符

我有一個包含用戶的全名和電子郵件和數據庫時,我想顯示他們兩個我有這樣的格式:

Christian Eric Paran <[email protected]>

,但我只顯示是這樣的:

Christian Eric Paran

有沒有辦法在PHP中顯示HTML?

回答

3

問題是,在<>使用由HTML分隔標記。因此,電子郵件地址最終被解析爲HTML標籤,並且如果不是所有的瀏覽器都會被隱藏。

使用&lt;在HTML中顯示<,在>中顯示&gt;

如果數據是動態的,請在打印之前使用htmlentitieshtmlspecialchars爲您進行上述編碼。

0

既然你正在處理的電子郵件地址,你肯定需要一定的靈活性處理如何顯示結果它不只是隱藏

我會建議http://php.net/mailparse_rfc822_parse_addresses

$email = mailparse_rfc822_parse_addresses("Christian Eric Paran <[email protected]>") ; 
    echo $email[0]['display'] ; // Christian Eric Paran 
    echo $email[0]['address'] ; // [email protected] 

標籤

如果您沒有安裝的郵件中分析你可以使用這個

$email = parse_addresses ("Christian Eric Paran <[email protected]>"); 
echo $email ['display']; // Christian Eric Paran 
echo $email ['address']; // [email protected] 

parse_addresses功能

function parse_addresses($address) { 
    $info = array(); 
    preg_match_all ('/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', $address, $matches); 
    $info ['display'] = $matches [1] [0]; 
    $info ['address'] = str_replace (array (
      "<", 
      ">" 
    ), "", $matches [2] [0]); 

    return $info; 
} 
相關問題