我正在尝试创建一个测验系统。我在数据库中有这个表:
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;
}