2012-01-24 101 views
0

已解決:閱讀下面的評論@Eray。PHP不執行功能

我有一個PHP函數來查看文本並將文本表情符號轉換爲圖像。 :),:(,:|等等,我也有一個函數,用文本查看並用HTML取代BBCode,我用數據庫中的字符串執行這些函數,兩者都使用全局變量$ newtext

emoticon($row['words']); 
bb($row['words']); 
echo "<b>" . $row['username'] . "</b> - " . $row['time']; 
echo "<p>" . $newtext . "</p>"; 
echo ""; 

關於這件事的奇怪之處在於,現在(我不記得我做了什麼)表情符號功能不起作用,但是bb函數確實無效,我的意思是它不能代替任何東西。文本仍文本。這已經工作過。此外,每天幾次,$ newtext自帶的用戶名之前。這裏是我的功能...

function emoticon($text) 
{ 
global $newtext; 
$newtext=str_replace(":)", "<img src='emoticons/smile.gif'>", $text); 
$newtext=str_replace(":(", "<img src='emoticons/sad.gif'>", $newtext); 
$newtext=str_replace(":D", "<img src='emoticons/biggrin.gif'>", $newtext); 
$newtext=str_replace(":p", "<img src='emoticons/tongue.gif'>", $newtext); 
$newtext=str_replace(":P", "<img src='emoticons/tongue.gif'>", $newtext); 
$newtext=str_replace(":|", "<img src='emoticons/neutral.gif'>", $newtext); 
$newtext=str_replace("8)", "<img src='emoticons/cool.gif'>", $newtext); 
$newtext=str_replace("8D", "<img src='emoticons/cool.gif'>", $newtext); 
$newtext=str_replace(":o", "<img src='emoticons/surprised.gif'>", $newtext); 
$newtext=str_replace(":O", "<img src='emoticons/surprised.gif'>", $newtext); 
$newtext=str_replace(";)", "<img src='emoticons/wink.gif'>", $newtext); 
$newtext=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $newtext); 
} 

function bb($text) 
{ 
global $newtext; 
$array=array(

"[b]" => "<b>", 
"[/b]" => "</b>", 

"[i]" => "<i>", 
"[/i]" => "</i>", 

"[u]" => "<u>", 
"[/u]" => "</u>", 

"[big]" => "<h1>", 
"[/big]" => "</h1>", 

); 
$newtext = str_ireplace(array_keys($array), array_values($array), $text); 
} 

你能不能解釋一下,或者幫助我?此外,有沒有更好的辦法比使用全球VA riables?我知道他們可能有點「危險」。

+5

沒有提供你的功能很難。使用全局$變量通常是一個壞主意,你應該避免這種情況。 – zaphod1984

+0

「不起作用」不提供任何有意義的信息。因爲我們不是psychics,所以你需要告訴我們表情()函數是什麼,你的代碼執行時會得到什麼樣的錯誤,你用什麼字符串測試你的函數,等等。 –

+2

+1 @ zaphod1984的評論。例如,您可以「返回」它,而不是使用全局變量。如果你分享你的功能,我們會幫助你。 'emoticon()和bb()' – Eray

回答

0
function emoticon($text) 
{ 
$smiley = array(
    ':)', 
    ':(', 
); 

$replace = array(
    "<img src='emoticons/smile.gif'>", 
    "<img src='emoticons/sad.gif'>", 
); 

    return str_replace($smiley, $replace, $text); 
} 
+0

給我一個「警告:爲foreach()在myfile.php 142行上提供的無效參數」 – CoffeeRain

+0

因此,'$ text'不是一個數組。只要一分鐘我就會爲你寫一個工作函數。 – Eray

+0

@CoffeeRain現在已經準備就緒。 – Eray

0

我想你最好這樣做:

$text = $row['words']; 
$text = emoticon($text); 
$text = bb($text); 
echo "<b>" . $row['username'] . "</b> - " . $row['time']; 
echo "<p>" . $text . "</p>"; 
echo ""; 

,然後編輯你的功能是這樣的:

function emoticon($text) 
{ 
    // remove this line: global $newtext; 
    $text=str_replace(":)", "<img src='emoticons/smile.gif'>", $text); 
    // etc... 
    $text=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $text); 

    return $text; 
} 

function bb($text) 
{ 
    // remove this line: global $newtext; 
    $array=array(
    // etc... 
    ); 
    return str_ireplace(array_keys($array), array_values($array), $text); 
} 
0

首先,定義你的PHP函數:

function emoticon ($string) 
{ 
    $emoticons = array(':)' , ';)'); 
    $icons = ('happy.gif','wink.gif'); 
    return str_replace($emoticons, $icons , $string); 
} 

function bb($string) 
{ 
    //BOLD [b]text[/b] 
    $string = preg_replace('/(\[b\]([\w\d\s\.]+)\[\/b\])/i','<b>$2</b>',$string); 

    //ITALIC [i]text[/i] 
    $string = preg_replace('/(\[i\]([\w\d\s\.]+)\[\/i\])/i','<em>$2</em>',$string); 

    //UNDERLINE [u]text[/u] 
    $string = preg_replace('/(\[u\]([\w\d\s\.]+)\[\/u\])/i','<u>$2</u>',$string); 

    return $string; 
} 

二,給你打電話PHP定義函數:

echo bb("[b]Bold[/b]"); //Return <b>Bold</b> 

粗體

echo bb("[i]Italic[/i]"); //Return <em>Italic</em> 

粗體

echo bb("[i]My [b]Text[/b][/i]"); //Return <em>My <b>Text</b></em> 

文本

$var = $_POST['foo']; 

echo bb($var); 

三,測試代碼:

emoticon($row['words']); 
bb($row['words']); 
echo "<b>" . $row['username'] . "</b> - " . $row['time']; 
echo "<p>" . $newtext . "</p>"; 
echo ""; 
+0

好吧,我試過回顯表情(「你好:)」),它的工作。儘管如此,它並不適用於最後一部分。 – CoffeeRain

+0

它的一個例子¬¬...嘿傢伙,創建你的代碼 –

-1

沒有測試它,但是這應該做到:

注意到,而不是使用global $newtext我創建一個局部變量並通過return將其返回到外部範圍。

function emoticon($text) { 
    $newtext=str_replace(":)", "<img src='emoticons/smile.gif'>", $text); 
    $newtext=str_replace(":(", "<img src='emoticons/sad.gif'>", $newtext); 
    $newtext=str_replace(":D", "<img src='emoticons/biggrin.gif'>", $newtext); 
    $newtext=str_replace(":p", "<img src='emoticons/tongue.gif'>", $newtext); 
    $newtext=str_replace(":P", "<img src='emoticons/tongue.gif'>", $newtext); 
    $newtext=str_replace(":|", "<img src='emoticons/neutral.gif'>", $newtext); 
    $newtext=str_replace("8)", "<img src='emoticons/cool.gif'>", $newtext); 
    $newtext=str_replace("8D", "<img src='emoticons/cool.gif'>", $newtext); 
    $newtext=str_replace(":o", "<img src='emoticons/surprised.gif'>", $newtext); 
    $newtext=str_replace(":O", "<img src='emoticons/surprised.gif'>", $newtext); 
    $newtext=str_replace(";)", "<img src='emoticons/wink.gif'>", $newtext); 
    $newtext=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $newtext); 

    return $newtext; 
} 

function bb($text) { 
    $array=array(
     "[b]" => "<b>", 
     "[/b]" => "</b>", 

     "[i]" => "<i>", 
     "[/i]" => "</i>", 

     "[u]" => "<u>", 
     "[/u]" => "</u>", 

     "[big]" => "<h1>", 
     "[/big]" => "</h1>", 
    ); 

    return str_ireplace(array_keys($array), array_values($array), $text); 
} 

$row['words'] = emoticon($row['words']); 

$row['words'] = bb($row['words']); 
+0

爲什麼downvote?至少你可以留下評論... – zaphod1984