假设将整个字符串存储到数据库中,并将句子拆分,选项和答案存储在自己的列中.
$example = ‘She went to the store to (buy,buys,bought,buying) some snacks when she (sees,see,saw,look) the accident.’;
管理将整个字符串存储到数据库中并将它们存储到相应的列中,并能够捕获第一个粗体文本并将其存储到数据库中.
但是无法捕获并将第二个粗体文本存储到数据库的列中.
define('DB_HOST', 'localhost');
define('DB_NAME', 'palmdatas');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$mode = $_POST['mode'];
$question = $_POST['editor1'];
$text = $question;
echo "\n";
$text = strip_tags($text);
function multiexplode ($delimiters,$string) {
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
$exploded = multiexplode(array(",","(",")"),$text);
$question = $_POST['editor1'];
$answer1 = extractString($question, '', '');
$sql="INSERT INTO strucquestions (part1,op1,op2,op3,op4,ans1,part2,op1a,op2a,op3a,op4a,ans2,part3,mode) VALUES ('$exploded[0]','$exploded[1]','$exploded[2]','$exploded[3]','$exploded[4]','$answer1','$exploded[5]','$exploded[6]','$exploded[7]','$exploded[8]','$exploded[9]','$answer2','$exploded[10]','$mode')";
echo "Successfully Inserted!";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
function extractString($question, $start, $end) {
$question = " ".$question;
$ini = strpos($question, $start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($question, $end, $ini) - $ini;
return substr($question, $ini, $len);
echo "close";
mysql_close($con);
}
?>
Next
解决方法:
Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.
这可能比使用strpos业务更优雅,因为在使用它时你必须循环直到你到达输入字符串的末尾.
标签:php,mysql
来源: https://codeday.me/bug/20190711/1434188.html