2010-07-14 89 views
0

看,如果論壇帖子中有$ hide_smilies設置爲1,我不希望:p,:o被替換爲圖片。PHP函數幫助/初學者

這是我輸出的論壇帖子bbcode($message);

和作用如何:

function bbcode($str) 
{ 
    $str = htmlentities($str); 

    $find = array(
    "/:p/", 
    "/:o/", 
    '/\[b](.*?)\[\/b]/is', 
    '/\[u](.*?)\[\/u]/is', 
    '/\[i](.*?)\[\/i]/is' 
    ); 

    $replace = array(
    '<img src="/images/forum/icon_tongue.gif" alt=":p" border="0" height="15" width="15">', 
    '<img src="/images/forum/icon_embarrassed.gif" alt=":o" border="0" height="15" width="15">' 
    '<strong>$1</strong>', 
    '<u>$1</u>', 
    '<i>$1</i>', 

    $str = preg_replace($find, $replace, $str); 



    return nl2br($str); 

感謝

編輯

function bbcode($str, $hide_smilies = 0) 
{ 

$str = htmlentities($str); 

$find = array(
    '/\[b](.*?)\[\/b]/is', 
    '/\[u](.*?)\[\/u]/is', 
    '/\[i](.*?)\[\/i]/is', 
); 


$replace = array(
    '<strong>$1</strong>', 
    '<u>$1</u>', 
    '<i>$1</i>' 
); 

if ($hide_smilies == 0) 
{ 
    $find[] = "/:p/"; 
    $find[] = "/:o/"; 

    $replace[] = '<img src="/images/forum/icon_tongue.gif" alt=":p" border="0" height="15" width="15">'; 
    $replace[] = '<img src="/images/forum/icon_embarrassed.gif" alt=":o" border="0" height="15" width="15">'; 
} 

$str = preg_replace($find, $replace, $str); 


return nl2br($str); 
} 

這工作,但現在(如果hide_smilies = 0)一些諸如「的字符被替換爲&quot;等等

+0

「現在這工作,但(如果hide_smilies = 0),如某些字符」獲取與"等取代了」你在非常肯定,當你隱藏的笑臉這只是發生?你調用ヶ輛()功能的頂部,這就是它的作用:http://php.net/manual/en/function.htmlentities.php – 2010-07-14 03:48:21

+0

嗯,我做了第一次我重新加載,也許它沒有重新加載好。 但你怎麼能讓你的輸出安全然後??如果你不能使用htmlentities – Basta 2010-07-14 04:05:53

+0

我並不是建議你不應該使用htmlentities()。基於該代碼,沒有理由,它應該發生只有當hide_smilies = 0。它應該發生在任何情況下這個問題可能是雙重編碼,也就是說,可能是字符串被傳遞了通過htmlentities()[或者具有類似功能的東西]不止一次。我也會確保這個HTML源碼本身沒有問題。 – 2010-07-14 05:06:01

回答

1

如果隱藏表情符號設置爲1,則只是回顯$ message而不是回顯bbcode($ message)。這裏是一個應該運行的一個簡單三元聲明:

echo ($hide_smilies==1) ? $message : bbcode($message); 
+0

我認爲他仍然需要做其他替代品。 – Faisal 2010-07-14 02:49:09

+0

但我仍然想要其他bbcodes – Basta 2010-07-14 02:51:17

+0

哦,然後將第二個參數傳遞給$ hide_smilies的bbcode函數,給它一個默認值false(或0)。在函數中,創建兩個替換陣列,一個用於表情符號,另一個用於其他表情符號。然後爲兩個數組執行preg_replace。如果我有更多時間,我會爲你解決它。 – 2010-07-14 02:55:59

0

只需使用array_slice()砍掉不必要的位。我假設你可以將$ hide_smilies變量傳遞給bbcode()函數。

<?php 
function bbcode($str, $hide_smilies=0) { 
    $str = htmlentities($str); 

    $find = array(
    "/:p/", 
    "/:o/", 
    '/\[b](.*?)\[\/b]/is', 
    '/\[u](.*?)\[\/u]/is', 
    '/\[i](.*?)\[\/i]/is', 
    ); 

    $replace = array(
    '<img src="/images/forum/icon_tongue.gif" alt=":p" border="0" height="15" width="15">', 
    '<img src="/images/forum/icon_embarrassed.gif" alt=":o" border="0" height="15" width="15">', 
    '<strong>$1</strong>', 
    '<u>$1</u>', 
    '<i>$1</i>', 
    ); 

    if ($hide_smilies) { 
     $find = array_slice($find, 2); 
     $replace = array_slice($replace, 2); 
    } 

    $str = preg_replace($find, $replace, $str); 

    return nl2br($str); 
} 
?> 
0

如果我理解正確的話,你還是要替換[B]的和[I]的用其HTML等價即使$ hide_smilies是1,對不對?在這種情況下,初始化每個陣列僅與非笑臉pattenrs然後添加額外的元件,如果$ hide_smilies = 1。例如:

// either pass in $hide_smilies, declare it global inside bbcode(), 
// or use $_GLOBALS['hide_smilies'] 
function bbcode($str, $hide_smilies) 
{ 
    $str = htmlentities($str); 

    $find = array(
    '/\[b](.*?)\[\/b]/is', 
    '/\[u](.*?)\[\/u]/is', 
    '/\[i](.*?)\[\/i]/is' 
    ); 

    $replace = array(
    '<strong>$1</strong>', 
    '<u>$1</u>', 
    '<i>$1</i>'); 


    if ($hide_smilies == 1) 
    { 
     $find[] = "/:p/"; 
     $find[] = "/:o/"; 

     $replace[] = '<img src="/images/forum/icon_tongue.gif" alt=":p" border="0" height="15" width="15">'; 
     $replace[] = '<img src="/images/forum/icon_embarrassed.gif" alt=":o" border="0" height="15" width="15">'; 
    } 

    $str = preg_replace($find, $replace, $str); 

    return nl2br($str); 
} 
+0

hm它的工作,但現在「取代"等 – Basta 2010-07-14 03:29:51

+0

這可能與你在你的函數開頭調用htmlentities()。如果你不想要特殊字符被他們的html實體等價物取代你可以評論但注意奇怪的字符沒有得到正確的編碼 – Faisal 2010-07-14 03:43:02

+0

但它沒有得到這樣的之前,我一直有在頂部htmlentities它只會被替換,如果你看網頁源。但現在我的所有帖子看起來都很瘋狂 – Basta 2010-07-14 03:46:04

0
function bbcode($str) 
{ 
    $str = htmlentities($str); 

    $find = array(
     '/\[b](.*?)\[\/b]/is', 
     '/\[u](.*?)\[\/u]/is', 
     '/\[i](.*?)\[\/i]/is' 
    ); 

    $replace = array(
     '<strong>$1</strong>', 
     '<u>$1</u>', 
     '<i>$1</i>', 
    ); 

    $str = preg_replace($find, $replace, $str); 

    return nl2br($str); 
} 
0

只是一個參數添加到功能和改變的方式您相應地構建$ find數組。

function bbcode($str, $hideSmilies = false) 
{ 

$find = array(
'/\[b](.*?)\[\/b]/is', 
'/\[u](.*?)\[\/u]/is', 
'/\[i](.*?)\[\/i]/is' 
); 

if (!$hideSmilies) 
{ 
    $find[] = "/:p/"; 
    $find[] = "/:o/"; 
} 
+0

hm它的作品,但現在我的文字字符,如「替換爲"等 – Basta 2010-07-14 03:30:31

+0

@Basta把我的代碼,並將其工作到你的函數。當然,用我所擁有的功能替換掉。對不起,我應該提到它不是我的文章中的完整代碼。 – 2010-07-14 03:33:09