已解決:閱讀下面的評論@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?我知道他們可能有點「危險」。
沒有提供你的功能很難。使用全局$變量通常是一個壞主意,你應該避免這種情況。 – zaphod1984
「不起作用」不提供任何有意義的信息。因爲我們不是psychics,所以你需要告訴我們表情()函數是什麼,你的代碼執行時會得到什麼樣的錯誤,你用什麼字符串測試你的函數,等等。 –
+1 @ zaphod1984的評論。例如,您可以「返回」它,而不是使用全局變量。如果你分享你的功能,我們會幫助你。 'emoticon()和bb()' – Eray