我試圖創建一個測驗系統。我有這個數據庫中的表:如何在我的測驗系統中正確使用php中的preg_replace函數
CREATE TABLE fill_gap_exercise (
quiz_id INT PRIMARY KEY,
text VARCHAR,
wordslist VARCHAR,
date DATETIME
)
這裏是從表中的示例數據行:
INSERT INTO fill_gap_exercise
(quiz_id, text, wordslist, date)
VALUES
(4, 'hello {0} up {1}', 'a:2:{i:0;s:5:"whats";i:1;s:5:"men??";}', '2012-10-29 13:24:25')
我試圖在text
領域內更換{}
人物,但我有一個問題,當句子有多次出現的序列時{}
。
這裏是我的代碼:
<?php
include('../connect_db.php');
$myData = mysql_query('SELECT * FROM fill_gap_exercise');
while ($row = mysql_fetch_array($myData, MYSQL_ASSOC))
{
$array = unserialize($row['wordslist']);
$text = implode('\r\n', $array);
echo "\n<br />";
$pattern = '/\{(.*)\}/';
$replace = '<input type="text" name="j_1" id="' . $row["quiz_id"] . '" />';
$subject = $row['text'];
$string = preg_replace($pattern, $replace, $subject);
print $string;
}
而且你的問題是什麼?什麼不行?你有沒有看到任何錯誤? – hakre
歡迎來到Stack Overflow! [**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護,[棄用過程](http://j.mp/Rj2iVR)已經開始。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –
請不要在這樣的關係數據庫中存儲序列化爲字符串的數據。使用'quiz_words'表來維持關係,這將使許多事情變得更容易。 – DaveRandom